简体   繁体   English

值未传递给组件-React Native

[英]Values are not being passed to a Component - React Native

I'm retrieving data from cloud firestore as an array of objects and I pass the object's values as props to another component: 我从Cloud Firestore检索数据作为对象数组,并将该对象的值作为prop传递给另一个组件:

    renderTips() {
        firebase.firestore().collection('pendingtips').get()
        .then(doc => { 
            doc.forEach(tip => {
                const tipData = tip.data();//array's object
                console.log(tipData.tip); //prints tip as expected
                console.log(tipData.name); //prints name as expected
                return <PendingTip key={tipData.tip} name={tipData.name} tip={tipData.tip} />; //doesn't returning enything
            });
        })
        .catch(() => Alert.alert('error'));
    }

    render() {
        return (
            <View style={styles.containerStyle}>
                <ScrollView style={styles.tipsContainerStyle}>
                    {this.renderTips()}
                </ScrollView>
            </View>
        );
    }

The array of objects looks like this: 对象数组如下所示:

{ name: 'Danny', tip: 'Be careful when crossing the road' },
{ name: 'Alex', tip: 'Drink water' }

The expectation is that in my ScrollView I will have a list of "tips". 期望在我的ScrollView中,我将有一个“提示”列表。 instead, I get nothing back as if the values are not being passed to the component. 相反,我什么也没有得到,好像值没有被传递到组件。

thanks in advance. 提前致谢。

RenderTips returns a promise which means it won't return anything at first render but only when the promise resolves. RenderTips返回一个promise,这意味着它在第一次渲染时不会返回任何内容,只有在promise解析后才返回。 You need setState in renderTips to tell react to re-render your component when data comes. 您需要在renderTips中使用setState来告知响应,以便在数据到来时重新呈现组件。 Make a seperate state array object for pendingTips then add the pendingTips component to that array and call setState 为endingTips创建一个单独的状态数组对象,然后将endingTips组件添加到该数组中并调用setState

this.state = { pendingTips: [] }

    componentDidMount() {
let pendingTips = []  // declare an array
        firebase.firestore().collection('pendingtips').get()
        .then(doc => { 

            doc.forEach(tip => {
                const tipData = tip.data();//array's object
                pendingTips.push(<PendingTip key={tipData.tip} name={tipData.name} tip={tipData.tip} />);  // push items in the array 
            });
this.setState({pendingTips})
        })
        .catch(() => Alert.alert('error'));
    }

    render() {
        return (
            <View style={styles.containerStyle}>
                <ScrollView style={styles.tipsContainerStyle}>
                    {this.state.pendingTips.map(tips => tips)}
                </ScrollView>
            </View>
        );
    }

You can solve this issue by setting doc as a state property and by moving the function for getting data into either some lifecycle method or effect hook. 您可以通过将doc设置为状态属性,并通过将用于将数据获取的函数移入某个生命周期方法或效果挂钩中来解决此问题。

You can try something like this: 您可以尝试如下操作:

    componentDidMount () {
        firebase.firestore().collection('pendingtips').get()
        .then(doc => { 
            this.setState({doc})
        }) 
    }

    renderTips() {
        const {doc} = this.state 
        return doc ? doc.map(tip => {
            const tipData = tip.data();
            return <PendingTip 
            key={tipData.tip} 
            name={tipData.name} tip={tipData.tip} />;
        }) : null
    }

    render() {
        return (
            <View style={styles.containerStyle}>
                <ScrollView style={styles.tipsContainerStyle}>
                    {this.renderTips()}
                </ScrollView>
            </View>
        );
    }

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

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