使用替换,重置和刷新的react-native-router-flux刷新组件

时间:2017-02-27 15:52:19

标签: javascript reactjs react-native refresh react-native-router-flux

所以我一直在使用包含react-native-router-flux的点火CLI。

我的问题是,我有一个来自道具的数据,当用户按“申请”时,这些数据将上传到数据库

<TouchableOpacity style={styles.button} onPress={this.onApply.bind(this)}> <Text style={styles.buttonText}>Apply</Text> </TouchableOpacity>

async onApply(){

var token = await AsyncStorage.getItem(STORAGE_KEY)
var url = "http://purwadhikaconnect.southeastasia.cloudapp.azure.com/api/applicants"
var jobId = this.props.jobData.Id
console.log('int?', jobId);
var config = {
  method: "POST",
  headers:{
    'Content-Type' : 'application/json',
    'Accept' : 'application/json',
    "Authorization" : 'Bearer ' + token
  },
  body: JSON.stringify({
    JobId : parseInt(jobId)
  })
}

return fetch(url, config).then((response) => response.json())
.then(this.setState({
  isApplied: true,
})).then((response) =>{Alert.alert('Job Applied Successfully')})
.then(this.setState({currentApplicant: this.state.currentApplicant + 1})) //trying to manipulate the value
.then(NavigationActions.Jobs({type: 'reset'}))
.catch((exception) => {Alert.alert('You have applied for this job!')})

}

现在数据已成功上传,但不会自动刷新。我一直在使用组件更新生命周期,例如shouldComponentUpdate() 你可以看到我的Action类型有reset。现在使用Actionconst.replace的问题是我的菜单按钮将被替换为后退按钮。如果我使用 ActionConst.refresh()它也不会刷新。请帮助我几个月来我一直在处理这个问题,我已经尝试了几乎所有的东西来解决它,但无济于事,令人沮丧......;(

1 个答案:

答案 0 :(得分:1)

我认为问题在于您将setStates作为您的.then的参数传递,尝试将其更改为:

.then((response) => this.setState({ isApplied: true }))