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 component should provide access to original #26

Closed
ivan-kleshnin opened this issue Apr 22, 2015 · 7 comments
Closed

Higher order component should provide access to original #26

ivan-kleshnin opened this issue Apr 22, 2015 · 7 comments

Comments

@ivan-kleshnin
Copy link

After this

@branch({
  cursors ...
})
class Foo extends Component {
}

and exposing proxied Foo there is no reference to original Foo. Since wrapper does not expose any method of original and does not provide a link to original, a bunch of useful techniques and strategies that were available with mixins become blocked. For example, data loading strategy described at Yomguithereal/baobab#170 is blocked because no property of original component can pass through this proxy.

Decorators use higher-order component (HOC) approach and in HOC that's the only place where original component is used:

render() {
  return <Component {...this.props} />;
}

I believe that HOC should still keep a link to a wrapped (original) component somehow.

This should be as easy as adding

var ComposedComponent = class extends React.Component {
  static original = Component; // +++
...

in both function root and function branch.

@Yomguithereal
Copy link
Owner

That's a sensible problem indeed. Do you know if any library is currently coping with this problem and how they might solve it?

@ivan-kleshnin
Copy link
Author

Do you know if any library is currently coping with this problem and how they might solve it?

Unfortunately, no.

@ivan-kleshnin
Copy link
Author

It was discussed in acdlite/flummox#173
@Gaeron proposes to solve it with decorators

function statics(a) {
  return b => Object.assign(b, a)
}

@statics({
  willTransitionTo(transition) {
    transition.abort();
  }
})
@connect({
  someStore: (store) => ({
    someProp: store.getSomeProp()
  })
})
export default class SomeRoute{
  render() {
    return <div>{this.props.someProp}</div>
  }
}

I've tried the same approach with Baobab-React and everything works fine.
You may want to mention this in docs somewhere.
So I'm gonna close this issue for now.

@Yomguithereal
Copy link
Owner

This seems a nice solution @ivan-kleshnin. I don't know if this already exist but I would love to see a tiny lib pack some of the more useful react-related decorators such as this @statics and @autobind.

@ivan-kleshnin
Copy link
Author

Never met one. I will share a link if find.

@prescottprue
Copy link

prescottprue commented Mar 29, 2017

I know this is old, but just incase anyone else was looking for a library:

This part of the react-docs points to a library called hoist-non-react-static, and it is used by react-redux's connect.

@Yomguithereal
Copy link
Owner

Hello @prescottprue. I don't remember if my HOC do this work properly. Do you want to open a PR?

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

No branches or pull requests

3 participants