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

Comments

@gaearon
Copy link

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

This comment has been minimized.

Copy link
Owner

commented Aug 4, 2016

Fixed. Thanks for the feedback @gaearon.

@krasimir krasimir closed this Aug 4, 2016

@gaearon

This comment has been minimized.

Copy link
Author

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
Projects
None yet
2 participants
You can’t perform that action at this time.