Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Should renderToString call life cycle methods? #2674
Came across this whilst investigating another unrelated issue (page requesting route twice). When we call renderToString on the server, React will try and call:
Calling mountComponent in turn triggers the life cycle method componentWillMount, which seems odd to me as it essentially isn't being mounted, it's only utilizing this method (as far as I can see) to generate the markup thus has a broken life cycle.
Not having a symmetrical life cycle can lead to odd issues with lingering objects e.g. when component listens for events outside it's own life cycle in componentWillMount and does not get a proper componentWillUnmount call, it does not get a chance to clean up it's references.
The main question is should the renderToString method invoke life cycle methods at all, as they'll never have a complete cycle? I understand we could just move bindings to componentDidMount but it still seems wrong to me to have an asymmetric life cycle. Perhaps we could block the call to componentWillMount by including
so it doesn't run when called server side?
Just some thoughts anyway :) really enjoying React!
It's true the "lifecycle" is not really a cycle on the server.
For your case with listeners and whatnot, it's probably better to set those up in