react-native - 代码推送检查更新

标签 react-native code-push react-native-code-push

我正在尝试使用代码推送更新我的应用程序。我的代码如下:

export default class codePushTest extends Component {
    constructor(props) {
        super(props);
        this.state = {
            updateAvailable: false,
            downloadingUpdate: null,
            installingUpdate: null,
            downloadProgress: null,
        }
    }

    componentDidMount() {
        let self = this;
        codePush.checkForUpdate().then(update => {
            if (!update) {
                self.setState({updateAvailable: false})
            }else{
                self.setState({updateAvailable: true})
            }
        })
    }

    handleUpdate() {
        let self = this;
        const checkUpdateStatus = (status) => {
            switch (status) {
                case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                    self.setState({downloadingUpdate: true});
                    break;
                case codePush.SyncStatus.INSTALLING_UPDATE:
                    self.setState({installingUpdate: true, downloadingUpdate: false});
                    break;
                case codePush.SyncStatus.UPDATE_INSTALLED:
                    self.setState({installingUpdate: false, downloadingUpdate: false, updateInstalled: true});
                    break;
            }
        };

        const downloadProgress = (downloadedBytes, totalBytes) => {
            self.setState({downloadProgress: (downloadedBytes / totalBytes) * 100})
        };

        codePush.sync({updateDialog: false, installMode: codePush.InstallMode.IMMEDIATE}, checkUpdateStatus, downloadProgress)
    }

    renderButton(){
        if (this.state.updateAvailable){
            return (
                <View style={{marginTop: 40}}>
                    <TouchableHighlight onPress={this.handleUpdate.bind(this)}>
                        <Text>An update is available.</Text>
                    </TouchableHighlight>
                </View>
            )
        }

    }

    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>Welcome message. </Text>
                {this.renderButton()}
            </View>
        )
    }
}

因此,我正在尝试检查 componentDidMount 中是否有可用的更新,如果是,则更新状态并根据该状态显示更新按钮。

但问题是,当我推送代码时,它是应用程序中的最新代码,它仍然显示按钮 An update available

如果我更改我的应用程序的内容并使用 code-push release-react AppName ios 将其发送到代码推送服务器,按钮再次显示,这很好。 如果我单击它,则会显示新内容并且按钮会消失,这很好。

但问题是,如果我刷新应用程序,我会再次获得旧内容并且按钮会再次显示。如果我再次点击它,什么也不会发生。

知道我做错了什么吗?

最佳答案

组件需要用codepush封装。我没有在文档中看到它。 Here is the link.

因此:

代替

export default Item;

一定是

let codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };
MainApp = codePush(codePushOptions)(Item);
export default MainApp;

关于react-native - 代码推送检查更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617963/

相关文章:

javascript - React Native Get Async Storage 返回未处理的 Promise

android - react native android 代码推送 installRelease 构建失败

reactjs - 了解react-native的代码推送

android - Codepush 不适用于所有 Android 用户

code-push - 代码推送中的暂存环境和生产环境之间有区别吗?

android - E/ react native : undefined is not an object (evaluating 'b.default.sync' )

android - Codepush React Native Android staging- 在根项目中找不到任务 installReleaseStagingDebug

react-native - react native 错误 : Unable to resolve module `./index` in node_modules/react-native

user-interface - 如何在React Native中实现卡片扩展到模态?

react-native - Realm React Native-如何在循环中更新多个对象