React native _this.state.data.map 不是函数

React native _this.state.data.map is not a function

在控制台中,我可以在渲染中得到 this.state.data。一切看起来都很正常。但是我得到 this.state.data.map is not a function 错误。我究竟做错了什么? 如果有人可以提供帮助,我将非常高兴。提前致谢

export default class ProfileScreen extends Component {

constructor(props) {
    super(props);
    this.state = {
        data: [],
        hash: '',
    };
}

_retrieveData = async () => {
    try {
        const value = await AsyncStorage.getItem('token');
        console.log(value);
        this.setState({
            hash: value,
        });
    } catch (error) {
        // Error retrieving data
    }
};

getInfo = async () => {
    try {
        const response = await axios.get('http://yakakartim.net/museb/kollektif/loginInfo?hash=' + this.state.hash);
        this.setState({
            data: response.data.message
        })
    } catch (e) {
        console.log(e)
    }
};

componentDidMount() {
    this._retrieveData();
    this.getInfo()
}

list = () => {
    return this.state.data.map(info => {
        return (
            <View style={{ margin: 10 }}>
                <Text>{info.email}</Text>
            </View>
        );
    });
};

render() {
    console.log('render',this.state.data)
    console.log('render',this.state.hash)
    return <View>{this.list()}</View>;
}

}

这是因为您正在更新数据变量,该变量最初是一个状态数组,但后来在 getInfo 函数中您已像这样更新它

 this.setState({
        data: response.data.message
    })

我不知道 "message" 里有什么。但是,如果它不是数组,则 map 函数将无法与 "data" 一起使用,因为它仅适用于可迭代的变量。我的意思是哪些是数组数据类型。

谢谢,传入的数据不是数组。我找到了这样的解决方案。

this.setState({
   data: [response.data.message]
})