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

Initialise state when getInitialState() is added after the initial render using HMR #38

Open
insin opened this issue Jan 8, 2016 · 2 comments

Comments

@insin
Copy link

insin commented Jan 8, 2016

When I'm working with hot reloading and adding state to a React.createClass component which didn't have it before, I usually have to hack in a one-time this.setState(this.getInitialState()) call somewhere to initialise the state and avoid TypeError: this.state is null in render().

Is it possible to detect that a getInitialState() has just been added to a component (or that state is null and getInitialState() exists, or some other heuristic) and have react-proxy do the setState(this.getInitialState()) bit for me, and is react-proxy the right place to do this?

@vramana
Copy link

vramana commented Jan 8, 2016

I think this is related to #15

@gaearon
Copy link
Owner

gaearon commented Jan 8, 2016

Yeah it's probably the right place. Feel free to try to PR this :-)

robertknight added a commit to robertknight/react-proxy that referenced this issue Jan 24, 2016
When replacing a proxied component that has no state with
one that does have state, renders and lifecycle methods that
depend on the initial state will typically fail with
'this.state is null' errors.

This addresses the issue by invoking getInitialState() on
classic components or running the constructor for modern components
and merging the resulting state with the existing state, with
the existing state taking priority.

Fixes gaearon#38
robertknight added a commit to robertknight/react-proxy that referenced this issue Jan 24, 2016
When replacing a proxied component that has no state with
one that does have state, renders and lifecycle methods that
depend on the initial state will typically fail with
'this.state is null' errors.

This addresses the issue by invoking getInitialState() on
classic components or running the constructor for modern components
and merging the resulting state with the existing state, with
the existing state taking priority.

Fixes gaearon#38
robertknight added a commit to robertknight/react-proxy that referenced this issue Mar 6, 2016
When replacing a proxied component that has no state with
one that does have state, renders and lifecycle methods that
depend on the initial state will typically fail with
'this.state is null' errors.

This addresses the issue by invoking getInitialState() on
classic components or running the constructor for modern components
and merging the resulting state with the existing state, with
the existing state taking priority.

Fixes gaearon#38
robertknight added a commit to robertknight/react-proxy that referenced this issue Mar 6, 2016
When replacing a proxied component that has no state with
one that does have state, renders and lifecycle methods that
depend on the initial state will typically fail with
'this.state is null' errors.

This addresses the issue by invoking getInitialState() on
classic components or running the constructor for modern components
and merging the resulting state with the existing state, with
the existing state taking priority.

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

No branches or pull requests

3 participants