我正在使用React Native的導航器。無論如何刷新組件,所以當我回到它,它會進行一個新的API調用,並獲取更新的數據顯示在組件中。我發現了一些類似的問題,但沒有好的答案...刷新導航器上的組件()
4
A
回答
1
您應該保存頁面的state
,並在componentDidMount
中發出一個動作,因爲它在組件裝入後立即被調用。
參考文獻:
- https://facebook.github.io/react/docs/react-component.html
- https://github.com/ReactTraining/react-router
ADDED
由於您的組件已被安裝,你應該聽ComonentWillReceiveProps
代替。
1
您可以將回調函數作爲道具發送到nextScene中的nextscene。
this.props.navigator.push({
name: *nextscene*,
passProps: {
text: response,
callBack: this.callback
});
async callback(){
await ....//make new api request grab the udpated data
}
然後在你的nextscene中調用回調方法,然後彈出。您還可以發送參數
this.props.callBack()
this.props.navigator.pop()
2
當pop()方法來刷新前一個頁面是不是一個好主意
您可以嘗試DeviceEventEmitter
對象
- 上一頁
DeviceEventEmitter.addListener('xxx', callback)
在componentDidMount
- 電流第
DeviceEventEmitter.emit('xxx', anythingInCallback...)
之前pop()
PS:前一頁DeviceEventEmitter.removeAllListeners('xxx')
在componentWillUnmount
0
我懷疑你還在尋找一個答案,但哇靠這已經讓我今晚。我對React Native很新,但我終於取得了一些成功。
React Navigation API docs有添加事件監聽器的部分!一探究竟!我也在下面分享了我自己的一些代碼。
這是組件中的示例事件處理程序,它是StackNavigator堆棧的頂部屏幕。它抓取當前狀態並使用API調用保存到後端。完成後,調用StackNavigator的彈出窗口。
handleSubmit =() => {
const { value, otherValue } = this.state
addThingToDatabase({ value, otherValue })
.then(() => this.props.navigation.pop())
}
現在轉到另一個組件,它是StackNavigator堆棧中「下方」的屏幕。這是在「流行」之後顯示的屏幕。以下是我在中曾使用過的ComponentDidMount中的。
componentDidMount() {
const { index } = this.props.navigation.state.params
getAllThingsFromDatabase({ index })
.then(({ arrayOfThings }) => this.setState({
index,
arrayOfThings
}))
}
但是,組件不會更新新的東西,直到addListener!現在我有幾乎相同的代碼,除了它在構造函數中。我想我只需要運行一次,我也需要存儲它。
constructor(props, context) {
super(props, context)
this.state = {
index: null,
arrayOfThings: []
}
this.willFocusSubscription = this.props.navigation.addListener(
'willFocus',
(payload) => {
const { index } = payload.state.params
getAllThingsFromDatabase({ index })
.then(({ arrayOfThings }) => this.setState({
index,
arrayOfThings
}))
}
)
}
請注意,文檔還提到使用.remove()
函數取消訂閱事件偵聽器。我把它放在ComponentWillUnmount()
。
componentWillUnmount() {
this.willFocusSubscription.remove()
}
有四種不同的事件要訂閱。我認爲willFocus
會在屏幕出現之前更新。
相關問題
- 1. 向上導航刷新加載器,但返回導航不
- 2. Force Titanium(iPhone)導航組窗口刷新
- 3. WP7 - 在導航上刷新ListBox GoBack()
- 4. 導航組件之間的導航重新加載導航器路線中的所有組件
- 5. 瀏覽器控件中刷新和導航功能的區別?
- 6. 在每個頁面刷新導航組件
- 7. 導軌導航和局部刷新
- 8. 導航抽屜中的刷新標題
- 9. 組件導航
- 10. 使用MVVM-Light刷新導航返回導航
- 11. 頁面刷新後導航上的活動狀態
- 12. 導航抽屜菜單上的Android刷新項目
- 13. 選項卡上的Bootstrap導航欄刷新更改
- 14. DurandalJS - 無視圖模式刷新導航
- 15. 返回導航/頁面刷新問題
- 16. 如何在React導航中刷新
- 17. 使用AngularJS刷新Bootstrap導航欄
- 18. Aurelia在導航時不'刷新'vm
- 19. 刷新導航抽屜碎片Android?
- 20. 刷新導航屬性在微風
- 21. 抽屜式導航欄刷新
- 22. 等待網頁導航和刷新
- 23. 我如何刷新bootstrap導航欄?
- 24. j導航刷新當前頁面
- 25. 導航到其他頁面刷新
- 26. 如何刷新JDesktopPane上的組件
- 27. 角度導航強制新組件
- 28. UITableView刷新導航控制器中的數據
- 29. Angular 2 rc.6導航到相同的路線不刷新組件
- 30. 左/右和上/下刷卡導航
上一個組件已經掛載,所以當你彈出它時不會再次調用componentDidMount。 –
這是有道理的,你應該能夠鉤住你的ComponentWillReceiveProps嗎? https://gist.github.com/agrcrobles/ba6832a3ecf8c8c47c0c4101aa0a2ffe – locropulenton
我想出了一個解決方案,讓它按照我想要的方式工作,只是希望有一個像其他生命週期方法一樣可以調用的OnFocus。 –