jedireza/aqua

Don't call actions in component constructors

jedireza opened this issue · 3 comments

Instead of calling actions to get data in component constructors, we should probably move those calls to componentDidMount().

componentDidMount()

[...]
If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

Via: https://facebook.github.io/react/docs/react-component.html#componentdidmount

Examples of calling actions in constructors:

I can take it, but why is constructor not the right place for the calls?


Is it done in all project components?

Or just a few?

I wouldn't say that the constructor is not the right place for the calls, but componentDidMount() is probably a better place.

I experienced a case where different components were firing actions in their constructor (causing changes in a redux store) and both getting state from the same store causing an invariant of modifying state during a render. After some digging I moved the action calls to componentDidMount() and that resolved the invariant issue.

Using componentDidMount() instead of the constructor may also be better for server side rendering because IIRC, the components don't "mount" on the server.

I've decided to archive this project. Thank you for your interest and contributions.
https://github.com/jedireza/aqua#no-longer-maintained