从React Native中的Promise返回JSX

我想尝试在我的React Native应用程序中有条件地渲染Components,但似乎没有任何效果。 永久违反:对象无效,无法作为React子对象(找到:对象w 第{_40,_65,_55,_72}个键)。如果您打算渲染孩子的集合,请改用数组。我不明白我在这里缺少什么

<RootContext.Consumer>
            {({handleChange,data}) => {
                let {property,currentFloor} = data
                let flag = false
                return this._getconfigDataFromFile(property,currentFloor)
                    .then(data => {
                        data.forEach(c => {
                            if (c.zone === this.props.title) {
                                flag = true
                            }
                        })

                        return (
                            <TouchableOpacity onPress={this._onPress}>
                                <View>
                                    {flag ? <SomeComponent /> : <SomeOtherComponent />}
                                </View>
                            </TouchableOpacity>
                        )
                    })
            }}
</RootContext.Consumer>
zz7630 回答:从React Native中的Promise返回JSX

原因是您需要返回React组件,而您返回的是Promise。 做类似的事情。

const App = (
     <ReactConsumer>
        <Component1 />  
     </ReactConsumder>
);

const Component1 = () => {
   React.useEffect(() => {
     const pr = await somePromise();
   },[]};

   return (
    { pr ? 
        <AnotherComponent>pr something</AnotherComponent> 
        : null or something else 
    });

您可以使用useEffect或诸如componentDidMount之类的东西。

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

大家都在问