I've encountered an issue with a function in my code that is supposed to change the screen and set the state simultaneously. Here's how it looks (initially, the weapon state is null):
var { navigate } = this.props.navigation;
clickM16 = () => {
this.setState({
weapon: "M16"
});
navigate("Second", {weapon: this.state.weapon});
}
When I try to access this on my second screen using {this.props.navigation.state.weapon}, the state doesn't update to M16 until I revisit the page and click the button again.
I have logged the console both before and after the setState function, and it consistently shows null on the first click but updates to M16 when clicked for a second time.
Is it not possible to run setState while navigating between screens? If it is, what could be the issue here?
TLDR: My goal is to adjust the state and switch pages within the same function so that I can display the new state as text on the following page. However, the state change only takes effect after the button is clicked twice.