-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Description
The answer written here is wrong. I think it is taken from this stack overflow link https://stackoverflow.com/questions/40199151/cant-set-state-in-componentwillmount. There reason behind setState not working was some parsing error. I think the answer for it need to be updated. I have tested it with the below code and it working properly with componentWillMount.
import React, { Component } from 'react';
import { Text } from 'react-native';
class Cat extends Component {
constructor(props) {
super(props)
this.state = {
name: 'Anand'
};
}
componentWillMount() {
setTimeout(()=> {
this.setState({name:'anand Kumar jha will'})
}, 3000)
}
componentDidMount() {
setTimeout(()=> {
this.setState({name:'anand Kumar jha did'})
}, 1000)
}
render() {
return (
Hello, I am your cat! {this.state.name}
);
}
}
export default Cat;