【问题标题】:Update state on successful update with code push使用代码推送成功更新状态
【发布时间】:2017-06-16 09:35:12
【问题描述】:

我正在使用 code-push 来更新我的 react-native 应用程序。我不想每次更新都上传到App Store,我想直接推送到用户设备上。

它工作正常,应用程序已更新,但如果更新成功,我会尝试显示通知。

docs 中,他们的做法如下:

codePush.sync({ updateDialog: true },
  (status) => {
      switch (status) {
          case codePush.SyncStatus.DOWNLOADING_PACKAGE:
              // Show "downloading" modal
              break;
          case codePush.SyncStatus.INSTALLING_UPDATE:
              // Hide "downloading" modal
              break;
      }
  },
  ({ receivedBytes, totalBytes, }) => {
    /* Update download modal progress */
  }
);

如果更新成功,我正在尝试更新状态并显示模态,并带有一个按钮以在单击时将其隐藏。因此我的代码如下:

constructor() {
        super();
        this.state = {
            modalVisible: false,
            status: ""
        }
    }

    componentDidMount() {
        let self = this;
        codePush.sync({
                updateDialog: true,
                installMode: codePush.InstallMode.IMMEDIATE
            }, (status) => {
                switch (status) {
                    case codePush.SyncStatus.UPDATE_INSTALLED:
                        self.setState({modalVisible: true});
                        break;
                }
            },
            ({receivedBytes, totalBytes,}) => {
                /* Update download modal progress */
                self.setState({status: "Downloaded " + receivedBytes + " of " + totalBytes});
            }
        );
    }

    renderUpdateNotificationModal(){
        return (
            <Modal
                animationType={"slide"}
                transparent={false}
                visible={this.state.modalVisible}
                onRequestClose={() => {alert("Modal has been closed.")}}
            >
                <View style={{marginTop: 22}}>
                    <View>
                        <Text>Updated</Text>
                        <TouchableHighlight onPress={() => {this.setState({modalVisible: false})}}>
                            <Text>OK</Text>
                        </TouchableHighlight>
                    </View>
                </View>
            </Modal>
        )
    }

    render() {
        return (
            <View style={styles.container}>
                {this.renderUpdateNotificationModal()}
                <Text style={styles.welcome}>
                    Welcome!
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.ios.js
                </Text>
                <Text style={styles.instructions}>
                    Press Cmd+R to reload,{'\n'}
                    Cmd+D or shake for dev menu
                </Text>
                <Text style={{fontSize: 18, marginTop: 15, color: "blue", textAlign: "center"}}>{this.state.status}</Text>
            </View>
        );
    }

但问题是模式显示了几秒钟然后消失了,没有点击按钮。

由于状态设置为true,然后在我几秒后变为false

case codePush.SyncStatus.UPDATE_INSTALLED:
     self.setState({modalVisible: true});
     break;

知道怎么解决吗?

【问题讨论】:

  • 安装更新后是否有闪屏?我认为您的应用已重新启动
  • @Jiro267 不。模态显示,然后一秒钟后消失,我看到更新的屏幕。屏幕不闪烁。
  • installMode: codePush.InstallMode.IMMEDIATE 这应该是根据github.com/Microsoft/react-native-code-push/blob/master/docs/… 强制应用重启。您确定它没有如此快速地重新启动和初始化(上面的应用程序非常小!)以至于您将其误认为是模式关闭和状态重置?似乎指向了这一点。解决方案是 codePush.InstallMode.ON_NEXT_RESTARTcodePush.InstallMode.ON_NEXT_RESUME

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


【解决方案1】:

我在加载页面中尝试使用 Codepush 时遇到了同样的问题,我的问题是 codePush 的 updateDialog 显示在下一个屏幕上。 因为 codePush.sync 是一个 Promise 函数,你可以使用它的 'then' 功能,它将确保以正确的顺序完成你的工作。

this demo 如果您想使用 CodePush,可能会有所帮助。您可以在 codePushStatusDidChange() 中相应地更改组件的状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2011-12-05
    相关资源
    最近更新 更多