I am trying to send request to api, and save one of the parameters I get from there inside my state, and then send it to another component to be used.
getCode function to send request, class method
getCode(text, psw) {
fetch('someurl', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({"telephone": text, "password": psw})
})
.then(response => response.json())
.then(response => {
//console.log(this.state.text) console.log(this.state.uid)
this.setState({uid: response["data"]["id"]
})
console.log(this.state.uid)
// this.setState({uid: response["data"]["telephone"]})
// console.log(this.state.uid);
})
}
Button which runs getcode function and navigates to another screen/component where I want to send my state
<Button
title="do it"
onPress=
{() => {this.props.navigation.navigate('SecondPage', {uid: this.state.uid}), this.getCode(this.state.text, this.state.password)} }/>
</View>
Here is my constructor and state of sender component
constructor(props) {
super(props);
this.state = {
text: '',
password: '',
password2: '',
uid: ''
}
}
Here is constructor and state of second component
constructor(props) {
super(props);
this.state = {
value: "",
focused: false,
text: "",
u_id: this.props.navigation.state.params.uid,
};
}
So the question is how do I send state to another component? tried couple tutorial but not working for me. And should I use props instead of state, since they are public? Please feel free to give advices on logic and code overall