无法访问状态,但我在调试器上看到了

我具有提取数据的功能,并且执行setState将数据放入自己的状态。

但是我不知道如何在render()

中使用它
getFixture = async () => {
    const url = 'http://127.0.0.1:80/testmatch/get_fixture.php';
    fetch(url).then((reponse) => reponse.json())
        .then((reponseJson) => { 
           this.setState({
                data:reponseJson,isLoading:false
            })
        })
    }
    componentDidmount(){
        this.getFixture()
    }

如果我在渲染中执行了console.log(this.state.data);,则在控制台中(在图像连接中)向我展示了这一点

如果我做console.log(this.state.data.elapsed);可以正常工作并且可以获取值,但是如果我做console.log(this.state.data.awayTeam.logo);可以得到 无法读取属性“徽标”

我不明白为什么。

Screenshot

这是所有代码,我想要的是使用从数据状态到渲染的所有数据:

getFixture = async () => {
        const url = 'http://127.0.0.1:80/testmatch/get_fixture.php';
         fetch(url).then((reponse) => reponse.json())
        .then((reponseJson) => { 
           this.setState({
                data:reponseJson,isLoading:false
            })
           console.log(this.state.data.awayTeam.logo); // WORKING (SHOW ME THE LINK)
        })
    }



render() {
   console.log(this.state.data.awayTeam.logo); // NOT WORKING (CANNOT READ PROPERTY OF UNDEFINED)
    return (
      <View style={styles.container}>
         <View style={styles.containerScore}>
            <View style={{flex: 1,flexDirection: 'row',borderColor:'#171F33',borderBottomWidth: 1 }}>

              <View style={{flex: 1,paddingTop:7,paddingBottom:7}}>
                 <View style={{flex: 1}}>
                 <Image style={{width: 50,height: 50}} source={{uri: "toto"}} />
                </View>
              </View>

              <View style={{flex: 1,paddingBottom:7,backgroundColor:'#fff'}}>
               <Text style={{fontSize:13}}> {this.state.data.elapsed}</Text>
              </View>

              <View style={{flex: 1,marginLeft:2,backgroundColor:'#000'}}>
               <Text style={{fontSize:13}}></Text>
              </View>

            </View>

          <Text style={{color:"#FFF"}}>{} </Text>
         </View>

         <View style={styles.containerInfo}>


           <ScrollabletabView style={{marginTop: 1}} initialPage={0} renderTabBar={() => <CustomTabBar/>} >

            <View tabLabel='Tab #1'>
             <Text>My</Text>
            </View>

            <View tabLabel='Tab #2'>
            <Text>favorite</Text>
            </View>

            <View tabLabel='Tab #3'>
            <Text>favorite</Text>
            </View>

            <View tabLabel='Tab #4'>
            <Text>favorite</Text>
            </View>

            </ScrollabletabView>
         </View>
      </View>




    );
  }


}

ping00000 回答:无法访问状态,但我在调试器上看到了

尝试像这样登录setState回调

document.getElementById("mkc").addEventListener("click",overlayShow);
,

在初始渲染中,this.state.data尚未设置,因此,如果您尝试不进行null检查就访问它,则会出现错误。

在您的jsx中,您可以检查this.state.data是否为假,是否为假,请立即返回:

  render() {

    if (!this.state.data) {
      return (<View><Text>Loading...</Text></View>);
    }

    console.log(this.state.data.awayTeam.logo); 

    return (
      <View style={styles.container}>
         <View style={styles.containerScore}>
            <View style={{flex: 1,flexDirection: 'row',borderColor:'#171F33',borderBottomWidth: 1 }}>
              <View style={{flex: 1,paddingTop:7,paddingBottom:7}}>
                 <View style={{flex: 1}}>
                 <Image style={{width: 50,height: 50}} source={{uri: "toto"}} />
                </View>
              </View>
              <View style={{flex: 1,paddingBottom:7,backgroundColor:'#fff'}}>
               <Text style={{fontSize:13}}> {this.state.data.elapsed}</Text>
              </View>
              <View style={{flex: 1,marginLeft:2,backgroundColor:'#000'}}>
               <Text style={{fontSize:13}}></Text>
              </View>
            </View>
          <Text style={{color:"#FFF"}}>{} </Text>
         </View>
         <View style={styles.containerInfo}>
           <ScrollableTabView style={{marginTop: 1}} initialPage={0} renderTabBar={() => <CustomTabBar/>} >
            <View tabLabel='Tab #1'>
             <Text>My</Text>
            </View>
            <View tabLabel='Tab #2'>
            <Text>favorite</Text>
            </View>
            <View tabLabel='Tab #3'>
            <Text>favorite</Text>
            </View>
            <View tabLabel='Tab #4'>
            <Text>favorite</Text>
            </View>
            </ScrollableTabView>
         </View>
      </View>
    )
}

A simple demo in codesandbox

本文链接:https://www.f2er.com/2978502.html

大家都在问