2017-06-18 193 views
3

我试图用代码推送更新我的应用程序。我的代码如下:代码推送检查更新

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,该按钮再次显示,这很好。 如果点击它,会显示新内容,并且按钮会消失,这很好。

但问题是,如果我刷新应用程序,我再次得到旧内容,并再次显示按钮。如果我再次点击它,没有任何反应。

任何想法,我究竟做错了什么?

回答

0

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

这样:

而不是

export default Item; 

它必须是

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