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

Comments

Projects
None yet
3 participants
@Jessidhia
Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor Author

commented Oct 24, 2018

Does this obsolete PureComponent?

@gaearon

This comment has been minimized.

Copy link
Member

commented Oct 24, 2018

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

@sebmarkbage

This comment has been minimized.

Copy link
Member

commented Oct 24, 2018

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

@gaearon

This comment has been minimized.

Copy link
Member

commented Nov 1, 2018

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

@gaearon gaearon closed this Nov 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.