简体   繁体   English

无法显示更新的状态(REACT-NATIVE)

[英]cant show updated state (REACT-NATIVE)

I want to show updated state name in the sidedrawer of my app,my functions work fine as I can see the name PRINTED twice in the console but for some reason I cannot show the name value on the app screen(shows starting value ""),I am thinking i might be using the wrong lifecycle method can someone help me? 我想在应用程序的侧边抽屉中显示更新的状态名称,我的功能正常运行,因为我可以在控制台中看到两次“打印”名称,但是由于某些原因,我无法在应用程序屏幕上显示名称值(显示起始值“”) ,我认为我可能使用了错误的生命周期方法,有人可以帮助我吗?

state = {
    name: {
        value: ""
    }

componentWillMount() {

getTokens((value) => {
        if (value[0][1] === null) {
            alert('whoops')
        } else {
            this.props.UpdateUserData(value[0][1]).then(()=>{
                console.log(this.props.User.userData)
            this.state.name.value = this.props.User.userData
            console.log(this.state.name.value)
            })
}
    })
}
 render() {
    return (

        <View style={styles.container}>
                <Text style={styles.displayname}>{this.state.name.value} 
                 </Text>
        </View>
    )
}

this.state.name.value = this.props.User.userData

不要直接改变您的状态,因为它不会重新渲染组件,而是使用setState方法,如下所示

this.setState({ name: this.props.User.userData });

A quote from react-bits 引用位的引用

Avoid async initialization in componentWillMount() 避免在componentWillMount()中进行异步初始化

componentWillMount() is invoked immediately before mounting occurs. 在挂载发生之前立即调用componentWillMount()。 It is called before render(), therefore setting state in this method will not trigger a re-render. 它在render()之前调用,因此在此方法中设置状态不会触发重新渲染。 Avoid introducing any side-effects or subscriptions in this method. 避免在此方法中引入任何副作用或订阅。

Replace componentWillMount with componentDidMount . componentWillMount替换componentDidMount Should work 应该管用

You are using this.state.name.value which is not correct. 您使用的this.state.name.value不正确。 It sets the state but doesn't actually cause the component to re-render itself. 它设置状态,但实际上并不会导致组件重新呈现自身。 Instead, you need to use the setState method to change the value of a state. 相反,您需要使用setState方法来更改状态的值。 This will make a call to re-render the component after the state has been changed. 状态更改后,这将调用重新渲染组件。 The example of that approach would be 这种方法的例子是

this.setState({ name: this.propos.User.userData });

Although this approach works but it's not advised to mutate the value of state. 尽管此方法有效,但不建议更改state的值。 States need to be considered immutable according to the guidelines. 根据准则,应将国家视为不变的。

componentWillMount does not differ much from constructor - it is also called once only in the initial mounting life-cycle. componentWillMount与构造函数没有太大区别-它仅在初始安装生命周期中被调用一次。 Many will be tempted to use this function in order to send a request to fetch data and expect the data to be available before the initial render is ready. 许多人会试图使用此功能来发送请求以获取数据,并期望在初始渲染准备就绪之前数据就可用。 This is not the case — while the request will be initialized before the render, it will not be able to finish before the render is called. 情况并非如此-尽管请求将在渲染之前初始化,但在调用渲染之前它将无法完成。

this.setState can solve the issue in this case. 在这种情况下,this.setState可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM