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

Document the "multiple componentWillMount" behaviour #22

Open
bradbarrow opened this issue Mar 8, 2017 · 2 comments
Open

Document the "multiple componentWillMount" behaviour #22

bradbarrow opened this issue Mar 8, 2017 · 2 comments

Comments

@bradbarrow
Copy link

bradbarrow commented Mar 8, 2017

Hey, thanks for your work on this.

I use componentWillMount to kick off some redux-thunks that do data fetching for my component on the server side.

Since react-async-component walks the tree to find the async components, it triggers all of the componentWillMount calls...then I still have to render the resulting appWithAsyncComponents with react-dom that invokes all of those componentWillMounts again.

The result is that I end up hitting my API twice for everything...once while finding the async components, and once when actually rendering to string.

UPDATE: I also find when using react-resolver that the same occurs. Essentially, any data fetching / expensive activity that's triggered by a pass over the tree would have to happen twice with this sort of approach.

@ctrlplusb
Copy link
Owner

Yeah, a difficult one to manage. Ideally I would love to be able to skip the componentWillMount call completely, but it's a part of the React server side contract and lots of people set up critical state within this lifecycle method for the render. We need this state to render and walk the tree correctly.

I have another library react-jobs which also makes use of the tree walking technique. I plan to create an integration layer so that the use of both does not result in a double tree walk. I can and do fire redux-thunk based actions in my own implementations using react-jobs. It's a bit more boilerplate but you can create util functions to cover your own use case.

Sorry, not looking for self promotion, but it's the only solution I know of thus far if you are wanting to do full server side rendering.

Full server side rendering is a pain. If SEO isn't absolutely critical to you my recommended approach is to use async components with the ssrMode: 'defer' option set. Render your app shell on the server for perceived performance, have everything else render client side. This doesn't fit everyones case of course, which I can appreciate. However I'd like to reiterate this as I often hear of people trying do SSR react without considering what parts of their app need to be SSR'ed and what other parts could be deferred. Code splitting is still a massive win.

@ctrlplusb ctrlplusb changed the title Multiple componentWillMount calls on server Document the "multiple componentWillMount" behaviour Mar 20, 2017
@gabemeola
Copy link

Could you use asyncBootstrap() from react-async-bootstrapper https://github.com/ctrlplusb/react-async-bootstrapper#naive-example ? Shouldn't this only be called once?

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

No branches or pull requests

3 participants