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

Higher order components should be wrapped before render() #12

Closed
gaearon opened this issue Aug 3, 2016 · 2 comments
Closed

Higher order components should be wrapped before render() #12

gaearon opened this issue Aug 3, 2016 · 2 comments

Comments

@gaearon
Copy link

gaearon commented Aug 3, 2016

This is not a good example:

var OriginalComponent = () => <p>Hello world.</p>;

class App extends React.Component {
  render() {
    return React.createElement(enhanceComponent(OriginalComponent));
  }
};

It is slow and blows away the DOM and state completely in all components below OriginalComponent because enhanceComponent(OriginalComponent) gives you a different type every time you render().

Here’s how to fix:

var OriginalComponent = () => <p>Hello world.</p>;
var EnhancedComponent = enhanceComponent(OriginalComponent);

class App extends React.Component {
  render() {
    return React.createElement(EnhancedComponent);
  }
};

The trick? We do this just once, so render() always returns the same type of EnhancedComponent.

@krasimir
Copy link
Owner

krasimir commented Aug 4, 2016

Fixed. Thanks for the feedback @gaearon.

@krasimir krasimir closed this as completed Aug 4, 2016
@gaearon
Copy link
Author

gaearon commented Aug 4, 2016

👍 Thanks for quick fix.

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

2 participants