react-router: React.lazy makes Route's proptypes fail
Hello,
The newly introduced React.lazy for easy code-splitting is making the Route component proptypes to fail with the warning :
Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function`.
Version
react-router-dom: 4.4.0-beta.4 react: 16.6.0
Test Case
I took the official example from here : https://reactjs.org/docs/code-splitting.html#route-based-code-splitting
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 58
- Comments: 28 (7 by maintainers)
Commits related to this issue
- Use latest react-router for compatibility with React.lazy https://github.com/ReactTraining/react-router/issues/6420 — committed to gladly-team/tab by kmjennison 5 years ago
@rnnyrk I just did the following for now 🤷♂️
@pshrmn hey,when will you publish 4.4.0 version,this is a common problem。
This was fixed in beta.5.
Wait for a release 😃
This trick does fix the error, but can have some side effect since it makes Home component re-render at every parent render. ( eg: if Home have componentDidMount )
Same thing with
React.memo().Any updates? It would be a great gift for the new year for me 😃
@vtereshyn it’s OK, I made a mistake that I didn’t explain in detail in my first comment. For others who worry about the case which @pierreferry mentioned, you can edit the demo code by entering
<Route exact path="/" render={props => <Home {...props} />} />and<Route exact path="/" component={props => <Home {...props} />} />to see the paint flashing zone@pierreferry if you go that path, the
renderprop would be better.Ah, sorry!
@prakashtsi works as expected. waiting for new release which is compatible with React.lazy to get rid of this code
This works for me. is it trustable @sunstorymvp ?
another option is to extend proptype definition for
Routecomponentprop (if not removed on build):should not have any side effects…
@willnew , oh, sorry, misunderstood. Okay, but its really a trick, so, I will wait for new release from react-router.
@vtereshyn I made a small Demo, you can test with it with
Paint flashingoption enabled on Chrome developer tool, the Home component didn’t get re-rendered every time its parent’s state change. so I think its a workaround, though it is a trick