简体   繁体   English

反应本地导航不承诺后呈现

[英]React native navigation not rendering after promise

On React Native, I'm using a Promise for the Navigator component on the RightButton function so that I can return a certain icon depending on whether a value in AsyncStorage returns true or false. 在React Native上,我对RightButton函数上的Navigator组件使用Promise,以便我可以根据AsyncStorage中的值返回true还是false来返回某个图标。 However the navigator always returns the next case in my switch case, as if it won't wait for my code to finish executing. 但是,导航器总是在我的切换大小写中返回下一个大小写,好像它不会等待我的代码完成执行一样。 Is there anything wrong with this implementation, or another way to check AsyncStorage before rendering out my Navigator RightButton. 此实现有什么问题,还是在呈现我的导航器RightButton之前检查AsyncStorage的另一种方法。

    switch(route.id) {
        case('businessProfile'):

            var _this = this;
            var p = new Promise(function(resolve, reject) {

                if (_this.checkBusinessFavourited()) {
                    resolve(true);
                } else {
                    resolve(false);
                }

            })
                .then(function(isFavourited) { 
                    if (isFavourited) {
                        return (
                            <View style={[ styles.multipleIcons ]}>
                                <TouchableOpacity
                                    onPress={() => {
                                        this.setBusinessAsFavourite(route.business.id)
                                    }}
                                    style={[ styles.navBarButton, styles.iconRightPaddingLarge ]}
                                    >
                                    <IconFA
                                        name='heart'
                                        size={ 25 }
                                        color='#de6262'
                                        style={ styles.icon }
                                    />
                                </TouchableOpacity>

                                <TouchableOpacity
                                    onPress={() => this.showShareActionSheet()}
                                    style={ styles.navBarButton }
                                    >
                                    <IconIon
                                        name='ios-upload-outline'
                                        size={ 25 }
                                        color='#696d6f'
                                        style={ styles.icon }
                                    />
                                </TouchableOpacity>
                            </View>
                        );

                    } else {

                        return (
                            <View style={[ styles.multipleIcons ]}>
                                <TouchableOpacity
                                    onPress={() => {
                                        this.setBusinessAsFavourite(route.business.id)
                                    }}
                                    style={[ styles.navBarButton, styles.iconRightPaddingLarge ]}
                                    >
                                    <IconFA
                                        name='heart'
                                        size={ 25 }
                                        color='#ddd'
                                        style={ styles.icon }
                                    />
                                </TouchableOpacity>

                                <TouchableOpacity
                                    onPress={() => this.showShareActionSheet()}
                                    style={ styles.navBarButton }
                                    >
                                    <IconIon
                                        name='ios-upload-outline'
                                        size={ 25 }
                                        color='#696d6f'
                                        style={ styles.icon }
                                    />
                                </TouchableOpacity>
                            </View>
                        );

                    }
                })

I've tried something more basic, but my 'checkBusinessFavurited' function gets called 4 times and doesn't always have a value passed, I guess because of race conditions. 我已经尝试了一些更基本的方法,但是我的'checkBusinessFavurited'函数被调用了4次,并且由于竞争条件的缘故,并非总是传递值。

if (this.checkBusinessFavourited(route.business.id)) {
    return (
        <View><Text>true</Text></View>
    )
} else {
    return (
        <View><Text>false</Text></View>
    )
}

I think the problem is that the switch statement doesn't return a component but a Promise. 我认为问题在于switch语句不返回组件而是Promise。 I think you need state to solve this. 我认为您需要状态来解决此问题。

getInitialState: function(){ return { isFavourited: false } }

Then, whenever you can check, maybe on componentWillMount, you do your async operation and as a result of that reset the state. 然后,只要您可以在componentWillMount上进行检查,就可以进行异步操作,并由此重置状态。

In the render function you then just use the state. 在渲染功能中,您只需使用状态即可。

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

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