gpt4 book ai didi

javascript - setState 方法未被调用

转载 作者:行者123 更新时间:2023-11-30 11:00:59 25 4
gpt4 key购买 nike

我的 setState 方法没有被调用,而打印 setState 之后的值总是打印默认值。我正在使用博览会

从'react'导入React;

从 './KeyboardShift' 导入 KeyboardShift;从 'react-native-floating-labels' 导入 FloatingLabel;

export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
focusDescriptionInput: false
}
}
render() {
return (
<SafeAreaView style={styles.container}>
<KeyboardShift>
{ () => (
<View>
<FloatingLabel
labelStyle={styles.labelInput}
inputStyle={styles.input}
style={styles.formInput}
returnKeyType={"next"}
onSubmitEditing={() => {
console.log(this.state.focusDescriptionInput)
context.setState({
focusDescriptionInput:true
})
console.log(this.state.focusDescriptionInput)
}}>
Email
</FloatingLabel>
<FloatingLabel
labelStyle={styles.labelInput}
inputStyle={styles.input}
style={styles.formInput}
focus={this.state.focusDescriptionInput}>
Email 2
</FloatingLabel>


</View>
)}

</KeyboardShift>
</SafeAreaView>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent:"center",
alignItems:"center",
marginTop: 50
},
textInput: {
fontSize: 16,
height: 40,
marginTop: 16
},
labelInput: {
color: '#673AB7',
},
formInput: {
borderBottomWidth: 1.5,
marginStart: 20,
marginEnd:20,
borderColor: '#333',
},
input: {
borderWidth: 0
}
});

最佳答案

使用 this.setState 代替 context.setState

并且在你的代码中如果你想在setState之后打印状态的值,它应该是:

            onSubmitEditing={() => {
console.log(this.state.focusDescriptionInput)
this.setState({
focusDescriptionInput:true
}, () => {
console.log(this.state.focusDescriptionInput)
})

}}>

为什么?请记住,setState异步。所以,在这个例子中我只是使用了setState的回调函数。希望这对您有所帮助!

关于javascript - setState 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com