Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it intended that React.memo() also works with class components? #13937

Closed
Jessidhia opened this issue Oct 24, 2018 · 5 comments
Closed

Is it intended that React.memo() also works with class components? #13937

Jessidhia opened this issue Oct 24, 2018 · 5 comments

Comments

@Jessidhia
Copy link
Contributor

Jessidhia commented Oct 24, 2018

Do you want to request a feature or report a bug?
Clarification in the documentation, or more strict input validation.

What is the current behavior?
Currently, React.memo() is documented to receive a functional component, but it actually works correctly even if given a class component. I discovered this while looking into the code for writing the types in DefinitelyTyped/DefinitelyTyped#29990.

The following code demonstrates that it works (both the rendering and the console.log(ref)):

const Test = React.memo(class extends React.Component {
  render () {
    return <h1>TEST</h1>
  }
})

ReactDOM.render(
  <Test ref={ref => { console.log(ref) }}/>,
  document.appendChild(document.createElement('div'))
)

What is the expected behavior?
Only functional components are documented as valid arguments, so either class components should also be documented as supported, or React.memo should reject the argument.

@gaearon
Copy link
Collaborator

gaearon commented Oct 24, 2018

Yeah we should probably document that it works with classes. It's intended to work with any valid component (including more exotic ones like lazy() result or forwardRef() result). But documenting this risks obscuring the primary use case (memoized function components).

@Jessidhia
Copy link
Contributor Author

Does this obsolete PureComponent?

@gaearon
Copy link
Collaborator

gaearon commented Oct 24, 2018

Not immediately, I think PureComponent is a bit more optimized right now. It could though. We'll see.

@sebmarkbage
Copy link
Collaborator

PureComponent is optimized for classes. It’s less optimized than pure + function.

@gaearon
Copy link
Collaborator

gaearon commented Nov 1, 2018

I'll close since it's intentional. Feel free to adjust docs in https://github.com/reactjs/reactjs.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants