State is not updating in componentWillMount
Date : November 01 2020, 12:01 AM
wish help you to fix your issue If you want to perform any asynchronous requests, I suggest you perform them in the componentDidMount lifecycle method. This is the suggested path based on the Reactjs documentation for componentDidMount.
|
Can't set state in componentWillMount
Date : November 14 2020, 11:01 PM
wish helps you In your case, your setState() won't work because you're using setState() inside an async callback Working Fiddle: https://jsfiddle.net/xytma20g/3/export default class Chat extends Component {
constructor(props){
super(props);
this.state = {
messages : [],
message : '',
};
this.socket = io('/api/');
this.onSubmitMessage = this.onSubmitMessage.bind(this);
this.onInputChange = this.onInputChange.bind(this);
}
componentWillMount() {
axios.get(`api/messages`)
.then((result) => {
const messages = result.data
console.log("COMPONENT WILL Mount messages : ", messages);
this.setState({
messages: [ ...messages.content ]
})
})
render(){
if(this.state.messages.length === 0){
return false //return false or a <Loader/> when you don't have anything in your message[]
}
//rest of your render.
}
};
|
componentWillMount fails to set the state
Date : March 29 2020, 07:55 AM
|
react: cannot set state in componentWillMount
Date : March 29 2020, 07:55 AM
|
State not mounting with componentWillMount
Date : October 09 2020, 01:00 AM
To fix this issue A few things here. Your state is not changing because the function in which you call setState has a different value for this than you're expecting. If you check the console, you should see an error indicating that this.setState is not a function. You need to change the following: window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
function(response) {
console.log(response)
window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
(response) => {
console.log(response)
|