javascript - ComponentDidMount react native 状态

标签 javascript reactjs react-native

我的代码:

   componentDidMount() {
       this.getEmail;
       console.log("checkpoint")
   }

   getEmail = () => {
       var email="something";
       this.setState({
           email: email,
       });
       console.log(this.state.email)
  }
  render() {
    return (
          <View>
            <Text style={styles.text} onPress={this.getEmail}>email :{this.state.email}</Text>
          </View>
    );
  }

控制台.logs:

//nothing happened?
checkpoint
(onPress Text into JSX)
this.state.email
something

所以我的函数运行良好,但 ComponentDidMount 不执行 getEmail,但如果我按“email :”,这会加载我的状态,一切都很好。

我希望 ComponentDidMount 执行我的函数

最佳答案

componentDidMount() {
    this.getEmail();
    console.log("checkpoint")
}

当然,按下文本时会执行 onPress={this.getEmail} ,但那是因为 onPress 事件监听器将 this.getEmail 变成了 this .getEmail()

关于javascript - ComponentDidMount react native 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949801/

相关文章:

javascript - 无法沿 y 轴向下移动对象 - Raphael、Javascript、SVG

javascript - componentDidMount 无法从父组件接收 props

android - react native Android : Creating a simple module

javascript - React Js - Flux 中的状态管理

javascript - 在嵌套导航器中导航时未定义 route.params?

android - onPanResponderRelease 不适用于 Android 设备

javascript - 在 javascript 中创建表单元素时调用函数

javascript - 如何在 Javascript 中增加对象的属性?

javascript/jQuery 分组元素

react-native-dropdown-picker 无法从 renderlistitem 中选择项目