-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
componentWillMount is called before render()? #7048
Comments
Does |
How can I control that? |
You can try and render a var Login = React.createClass ({
getInitialState: function(){
return {
loggedIn: false,
loaded: false
};
},
componentWillMount: function() {
[ ... some firebase code ... ]
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
this.setState({ loggedIn: true, loaded: true });
}
else {
this.setState({ loggedIn: false, loaded: true });
}
}
render: function() {
if (!this.state.loaded) return <Loading />;
return (
<div>
{this.state.loggedIn ? (
<div>
<Backend/>
</div>
) : (
[ ... login form.... ]
) }
</div>
);
}
}); // END LOGIN That way you don't show either screen until you've resolved the actual auth state for the user. Otherwise, you can't really control the firebase API to that extent, it's likely inherently asynchronous as it has to make an HTTP request before it resolves, which is almost always going to occur after your component renders. |
Yeah, sounds like your This is effectively a request for #6481 and #1739. Closing as a duplicate of those two issues. The workaround suggested by @aweary is currently what @sebmarkbage generally recommends. |
Hmm, seems like code below this.sate({}) is completely ignored in my example… what could cause that? |
@mnbucher setState shouldn't be completely ignored, but it may be asynchronous. There is a big red notice in the documentation (https://facebook.github.io/react/docs/component-api.html#setstate) that says:
Maybe that's what you're running into? |
I have encountered the same problem, you should try this solution, it solved the problem for me.
|
selector fields in filter bar now have selections based on keys in corresponding summaryData object; fixed issue with render happening before WillMount that breaks everything, render Loading.js until summaryData is ready (facebook/react#7048)
Hi together
Learning React at the moment and not sure if I'm not unterstanding a core part of React or this is a bug...
My idea was to create a simple login and don't show the login window if the user is already logged in. Authentication is handled via Firebase, so here's a small snippet of my code:
So my problem is the following: componentWillMount should be called before render, so if the user is already logged in, the new state is loggedIn: true and so it only renders the backend and not the login form... but unfortunately this new state in componentWillMount is not taken before render and it actually renders the login form
Thanks for the answer, cheers!
Martin
The text was updated successfully, but these errors were encountered: