Pass props to component#1
Pass props to component#1didierfranc merged 2 commits intodidierfranc:masterfrom perzi:feature-pass-props-to-component
Conversation
|
Indeed it could be great to pass props, I never needed it but it's a basic we should support. I think it will be better like the following, if you make the changes I'll merge this PR. PS: don't forget to build the lib, some people like to have git commit as dependencies ;) class Async extends React.Component {
componentWillMount = () => {
this.props.load.then((c) => { this.C = c; this.forceUpdate() })
}
render = () => this.C ? <this.C.default {...this.props} /> : null
}
Async.propTypes = {
load: React.PropTypes.instanceOf(Promise).isRequired,
}const Home = props => <Async load={import('./Home')} {...props} /> |
|
@didierfranc Updated PR with built lib. With my solution there's no risk of conflicting prop names, the lazy loaded component can also have a prop named load. In your example, if you pass a load prop to Home, that will be used instead of the promise. Another solution would to rename In general I prefer not pass any props only intended for a parent component to a child component. It's a risk of passing to much and leading to unforseen consequences. |
|
Yes I was thinking about that risk too, thanks for that ! |
Async can pass componentProps to the lazy loaded component. For instance if you need the react router params in props.