无法将数据从状态渲染到React组件中

时间:2019-12-03 18:32:08

标签: reactjs react-native state

我试图调用Firebase并以JSON的形式获取数据,然后将JSON数组的对象传递给其他其他react-native组件。

我能够成功调用Firebase,然后获取JSON对象数组。然后,稍后将该JSON对象列表存储在STATE变量中,然后稍后尝试将对象一个接一个地传递给另一个react-native组件。

代码:

class DisplayCardInformation extends Component 
{
const itemsRef = db.ref('firebaseLink/');

state = {
        listDataFromDB: null,
        lastKey: null
    };
componentDidMount() 
    {
        itemsRef.on('value', ( snapshot ) => {
            var data = snapshot.val();

            this.setState({
                listDataFromDB : snapshot.val(),
            });

            var keys = Object.keys(data);
            keys.forEach((key) => 
            { 
                this.setState({
                    lastKey: key
                });
            });
        });
    }

render() {

        this.state.listDataFromDB.map((listDataItem) => {
            return(
                <CardInformation listItem = {listDataItem} />
            );
        });     
    } 

}

export default DisplayCardInformation;

我正在尝试将信息传递给CardInformation。

错误:

TypeError: TypeError: null is not an object (evaluating 'this.state.listDataFromDB.map')

This error is located at:
    in DisplayCardInformation (at Home.js:33)

1 个答案:

答案 0 :(得分:1)

您需要将状态的初始值设置为任何数组,例如:

import tensorflow as tf

with tf.compat.v1.Graph().as_default():
    x1 = tf.constant([1,2,3,4])
    x2 = tf.constant([5,6,7,8])
    result = tf.multiply(x1, x2)
    with tf.compat.v1.Session() as sess:
        output = sess.run(result)
        print(output)
        # [ 5 12 21 32]

您遇到的问题是,当您的组件首次呈现时,它将state = { listDataFromDB: [] }; 传递给子组件。