My attempts to change the value of the state "id" with setState doesn't work. I've tried using ordinary setState, which doesn't update the value of the state. So I tried a functional setState, but it still doesn't work. The alert shows 0, instead of 5.
Why is it not updating?
class Form extends Component {
constructor(props){
super(props);
this.state = {lat: null,
lng: null,
radius: null,
id: 0,
filename: ''};
}
componentDidMount() {
function setStateFunction(state, props) {
const newState = {...state, id: 5};
return newState;
}
this.setState(setStateFunction);
alert(this.state.id);
}
render(){
return (<div>...</div>);
}
}