类型错误:null不是对象(评估'this.state.isLoading')

时间:2018-06-18 21:28:40

标签: listview react-native react-native-android

我初步确定了状态,但发生了什么

  • 已经导入了nessesary stuffs

    import React, { Component } from 'react';
    import { AppRegistry, FlatList, StyleSheet, ActivityIndicator ,ListView , Alert , Text, View } from 'react-native';
    
    export default class FlatListBasics extends Component {
    
       constructor(props) {
    super(props);
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.states = {
      isLoading : true ,
    dataSource: {}
    };
    }
       componentDidMount = () => {
          fetch('https://jsonplaceholder.typicode.com/posts/1', {
             method: 'GET' ,
          })
          .then((response) => response.json())
          .then((responseJson) => {
             console.log(responseJson);
             Alert.alert("done");
             this.setState({
                data: responseJson ,
                isLoading: false
    

    })       })

    .catch((error)=> {          console.error(误差);

    });        }       render(){         如果(!this.state.isLoading){           回来(                  );         }         其他{           回来(                   {item.body}}             />                      );         }

      }
    }
    
    const styles = StyleSheet.create({
      container: {
       flex: 1,
       paddingTop: 22
      },
      item: {
        padding: 15,
        borderBottomWidth: 2 ,
        borderBottomColor: '#888888' ,
        fontSize: 18,
        height: 54,
      },
    })
    
  • 我刚刚调用了this.state

  • 为什么现在这样的人发生了

1 个答案:

答案 0 :(得分:0)

乍一看,您似乎已在构造函数中将this.state拼写为this.states ...

...
this.states = {
  isLoading : true ,
  dataSource: {}
};...

this.states更改为this.state:)