question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generates class while README suggested functional component

See original GitHub issue

Subject of the issue

If you try to generated code from a string of Markdown using the @mdx-js/mdx-package it’s returning a class component instead of a functional component as suggested in the README file.

Your environment

ProductName: Mac OS X ProductVersion: 10.14.2 BuildVersion: 18C54

@mdx-js/mdx: 0.16.6 Node: v11.6.0 NPM: 6.5.0 Yarn: 1.12.3

Steps to reproduce

I am trying to the following:

  let transformedFunction = mdx.sync(`
  # Hello, MDX
  
  I <3 Markdown and JSX
  `)

this returns:


export default class MDXContent extends React.Component {
  constructor(props) {
    super(props)
    this.layout = null
  }
  render() {
    const { components, ...props } = this.props

    return <MDXTag
             name="wrapper"
             
             components={components}><MDXTag name="h1" components={components}>{`Hello, MDX`}</MDXTag>
<MDXTag name="p" components={components}>{`  I <3 Markdown and JSX
`}</MDXTag>
           </MDXTag>
  }
}

Expected behaviour

I would expect it returns the suggested code as listed in the README file:

export default ({components, ...props}) => <MDXTag name="wrapper"  components={components}><MDXTag name="h1" components={components}>{`Hello, MDX`}</MDXTag>
<MDXTag name="p" components={components}>{`I <3 Markdown and JSX`}</MDXTag></MDXTag>

Actual behaviour

See above

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
johnocommented, Jan 5, 2019

Of course 😄.

The reason is the previous functional version evaluated layouts inline of the render function. This caused the layout component to remount on every render if you declared a layout as an inline function like so:

# Hello, world

export default props => <div {...props}>

This caused unnecessary rerenders, but more importantly also broke things like hash links due to remounting.

By using a class-based component we memoize it as an attribute in the class. Though, I suspect we can now do this more cleanly with React.memo which was released in v16.6 🤷‍♀️

Hope that helps!

0reactions
johnocommented, Mar 6, 2019

Going to close this since it’s being tracked in #389 as well. v1 will move back to a functional component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Convert React Class Components to Functional ...
In this tutorial, we'll look at five ways to convert React Class Components to Functional Components using React Hooks.
Read more >
Airbnb React/JSX Style Guide - javascript - GitHub
This style guide is mostly based on the standards that are currently prevalent in JavaScript, although some conventions (i.e async/await or static class...
Read more >
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >
Documenting components - React Styleguidist
Styleguidist generates documentation for your components based on the comments in your source code, propTypes declarations, and Readme files.
Read more >
React Markdown component: the easy way to create rich text
The basic idea is that you add the component into your render function, and can write Markdown in between your opening and closing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found