这是一种方法,您可以看到许多设置为export default class App extends React.Component { render() {
return (
<View style={styles.container}>
<TextInput style={styles.box}/>
</View>
); }}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
box: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
backgroundColor: 'red'
},
});
的行,但是它们不起作用。
undefined
您会看到,值未重置:
我正在调试,但是它没有重置值,为什么?我想念什么吗?这是全班学生:
handleDismissMenuAndLogOut = () => {
this.setState({
buyOrRelease: "buy",
anchorEl: undefined,
isLoggedIn: undefined,
userID: undefined,
name: undefined,
email: undefined,
picture: undefined
}, this.props.userLoginAndDataDownloadCompletedOut(this.state));
};
答案 0 :(得分:3)
setState是异步的,这意味着直接在setState函数之后运行的代码将无法访问新状态。您必须使用setState的第二个参数来访问新状态。
this.setState({
isLoggedIn: false,
name: "",
buyOrRelease: "buy",
anchorEl: undefined,
isLoggedIn: undefined,
userID: undefined,
name: undefined,
email: undefined,
picture: undefined
}, this.props.userLoginAndDataDownloadCompletedOut);
this.setState({
key: value,
// This new state is shallow merged with the old one, if you dont specify an existing key, the existing key and value will remain the same.
}, (newState) => {
newState.key === value; // newState contains the new complete state.
});