繁体   English   中英

用状态反应本机更改变量

[英]React Native Changing Variable with State

我是新来的反应者,我只是开始玩弄状态和道具。 我一直在尝试获取一些数据,然后在其中填充一些卡视图。
到目前为止,这是我的代码的样子:

class Two extends React.Component {
constructor(props) { 
    super(props);
    this.state = { 
      day: "Monday",
    };
  }

// Getting monthly schedule
    getSchedules = () => {
        fetch("http://xxxxxx/getschedules.php", {method: "GET"})
        .then((response) => response.json())
        .then((responseData) => {
            AlertIOS.alert(
                "GET Response",
                "Search Query -> " + responseData.result.length)
                this.setState({day: responseData.result[1].day}); //changing state but nothing changes
        })
        .then((data) => { 
         console.log(data);
        })
        .catch(function(err) {
         console.log(err);
        })
        .done();
    }

  render() {  
            <TouchableOpacity onPress={this.getSchedules}>
              <View style={styles.card}>
              <Text>{this.state.day}</Text>
              </View>
              </TouchableOpacity>
             }
}

如果单击卡,则文本值应该会更改,但是什么也不会发生。 还有一种方法可以在页面加载时自动更改状态,而无需单击卡? 任何帮助将不胜感激!

在您的onPress代码中: <TouchableOpacity onPress={this.getSchedules}>

它应该是<TouchableOpacity onPress={this.getSchedules.bind(this)}>

通过仅传递函数引用, this上下文不会传递给getSchedules。

您是否检查过提取是否正常?

我用一个Facebook示例网址尝试了您的代码,

网络请求失败

在控制台中( CMD + D调试)

遵循此SO答案中的指示将域添加到info.plist的例外列表中,并且它起作用:)

info.plist文件位于Xcode中,您可以通过右键单击并选择“打开为>源代码”来更轻松地进行更新-这样,您就可以从上述SO答案中复制并粘贴代码

暂无
暂无

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

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