问题描述
我是 React Native 的新手.我们如何在通过调用 goBack()
返回时刷新/重新加载前一个屏幕?
I am new to React Native. How can we refresh/reload previous screen when returning to it by calling goBack()
?
假设我们有 3 个屏幕 A、B、C:
Lets say we have 3 screens A, B, C:
A -> B -> C
当我们从屏幕 C 运行 goBack()
时,它返回到屏幕 B,但具有旧的状态/数据.我们怎样才能刷新它?构造函数不会被第二次调用.
When we run goBack()
from screen C it goes back to screen B but with old state/data. How can we refresh it? The constructor doesn't get called 2nd time.
推荐答案
是的,构造函数只调用第一次,不能调用两次.
Yes, constructor is called only for the first time and you can't call it twice.
第一:但是您可以将数据 getter/setter 与构造函数分开并将其放入一个函数中,这样您就可以将函数向下传递到下一个场景,并且无论何时返回你可以简单地回忆一下这个函数.
First: But you can separate the data getter/setter from the constructor and put it in a function, this way you can pass the function down to the next Scene and whenever you're going back you may simply recall the function.
更好:您可以在第一个场景中创建返回功能,该功能在返回时也会更新场景并将返回功能向下传递.这样第二个场景就不会知道你的更新函数是合理的.
Better: You can make a go back function in your first scene which also updates the scene while going back and pass the go back function down. This way the second scene would not be aware of your update function which is reasonable.
最佳:您可以在第二个场景中使用 redux 并发送返回动作.然后在您的减速器中,您负责返回 &刷新你的场景.
Best: You can use redux and dispatch a go-back action in your second scene. Then in your reducer you take care of going back & refreshing your scene.
这篇关于在 goBack() 上刷新上一个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!