Generates class while README suggested functional component
See original GitHub issueSubject 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:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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!
Going to close this since it’s being tracked in #389 as well. v1 will move back to a functional component.