[英]React hook state updated in parent but child component does not receive new props
[英]React Native child component does not receive updated state from parent
我正在尝试实现一个应用程序登录屏幕,在该屏幕上,用户输入用户名和密码,并且该应用程序将检索Json Web令牌以将其存储在应用程序状态中,以便在服务器上管理其帐户。 现在,我的App.js如下所示
const RootStack = StackNavigator(
{
LoginPage:{screen:LoginPage},
HomePage:{screen:HomePage},
},
{headerMode:'none'},
);
export default class App extends Component{
constructor(props) {
super(props);
this.state = {token:'not available yet'}
}
updateState = async(u,p) => {
try {
let token = await this.getToken(u,p);
let merchantInfo = await this.getMerchantInfo(token);
this.state = {token: token, merchantInfo}
console.log(this.state)
console.log('our token state is '+ this.state.token)
console.log("our merchantinfo state is "+this.state.merchantInfo.name)
}
catch (error) {console.log(error);}
}
// update the state of merchant using authToken
getMerchantInfo = async(authToken) => {
var options = {
"method": "GET",
"headers": {
"authorization": "Bearer " + authToken,
"content-type": "application/json"
}
}
try {
let response = await fetch('http://example.com/merchants/',options);
let responseJson = await response.json();
return responseJson;
} catch (error) {console.error(error);}
}
// send over the username and password to server to retrieve an authToken
getToken = async(u,p) => {
console.log('username is: '+u+' password is: '+p)
var options = {
"method": "POST",
"headers": {
"content-type": "application/json"
},
"body": JSON.stringify({
"email": u,
"password": p,
}),
};
try {
let response = await fetch('http://example.com/merchants/login',options);
let responseJson = await response.json();
return responseJson.token;
} catch (error) {console.error(error);}
}
render() {
return (<RootStack screenProps= {this.state} />)
}
}
我的HomePage.js如下所示
export default class HomePage extends Component {
constructor(props) {
super(props);
this.state = {isReady: false};
console.log ('HomePage gets this token state: ' + this.props.screenProps.token)
}
render() {
return (
<Container>
<Header hasTabs>
<Left />
<Body>
<Title>my app</Title>
</Body>
<Right>
<Button transparent onPress={() => this.props.navigation.navigate('LoginPage')}>
<Text>Logout</Text>
</Button>
</Right>
</Header>
<Tabs>
<Tab heading="Issue">
<IssueTab />
</Tab>
<Tab heading="Redeem">
<RedeemTab />
</Tab>
<Tab heading="Settings">
<SettingsTab />
</Tab>
</Tabs>
</Container>
);
}
}
当我在App.js中console.log(this.state.token)时,我从服务器获取了正确的JWT令牌:
our token state is ayJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.fgJuYW3lIjoibG92ZXNoYWNrIiwiZW1haWwiOiJhQGIuY29tIiwiaW1hZ2UiOiJ4cGxvYWRzL0RlZmF1bHQucG8vIiwibWVyY2hhbnLJZCI6IjViMDBjZGZlNzMwODA4NmE0YjZiOWM3NSIsImlhdCI6MTUyNzgwMjU1MywiZXhwIjoxNTU5MzYwMTUzfQ.0QQStfMyDcmYeqeFToei5M4Q35dj43S05NiI0uosXTg
但是,当我在HomePage.js中console.log(this.props.screenProps.token)时,我得到了初始状态:
HomePage gets this token state: not available yet
我从此处进行搜索尝试过componentWillReceiveProps和其他一些方法,但是对于我的情况没有任何作用。 在我的情况下,如何从HomePage.js从App.js获取更新状态?
这是我在StackOverflow上的第一篇文章。 预先加油,谢谢!
状态只能在构造函数内部直接设置,例如this.state = { some: 'state' }
。 在构造函数之外,您必须通过设置程序this.setState
。 setState
触发重新渲染,新状态将按预期传递。 请在此处查看状态文档。
...
updateState = async(u,p) => {
try {
let token = await this.getToken(u,p);
let merchantInfo = await this.getMerchantInfo(token);
this.setState({token: token, merchantInfo})
}
catch (error) {console.log(error);}
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.