那是因为屏幕已经安装并且初始参数不会更新。不过,您可以做的是创建一个使用“react-native-navigation”提供的“withNavigationFocus”增强的包装器组件。
https://reactnavigation.org/docs/1.x/with-navigation-focus/
ComponentWithFocus
import React, {Component, useState} from 'react';
import { withNavigationFocus } from 'react-navigation';
const ComponentWithFocus = (props) => {
const {isFocused, onFocus, onBlur} = props;
const [focused, setFocused] = useState(false);
if(isFocused !== focused) {
if(isFocused) {
typeof onFocus === 'function' ? onFocus() : null;
setFocused(true)
} else {
typeof onBlur === 'function' ? onBlur() : null;
setFocused(false)
}
}
return (
props.children
)
}
export default withNavigationFocus(ComponentWithFocus);
并像这样在屏幕上使用它:
...
onFocus = () => {
//your param fetch here and data get/set
this.props.navigation.getParam('param')
//get
//set
}
...
render() {
<ComponentWithFocus onFocus={this.onFocus}>
/// Your regular view JSX
</ComponentWithFocus>
}
注意:如果参数仍未更新,则应重新考虑导航方法。例如,不需要像这样从 tabBar 导航:
navigator.navigate("Stackname" {screen:"screenname", randomProp: "seomthing")
您可以改为执行以下操作:
navigator.navigate("screenName", {'paramPropKey': 'paramPropValue'})
这将起作用,因为“.navigate”函数会找到与名称匹配的第一个可用屏幕,如果尚未安装,则将其安装到堆栈上(触发 componentDidMount 方法)。如果屏幕已经存在,它只是导航到它,忽略 'componentDidMount' 但传递 'isFocused' 道具,幸运的是,我们在 'ComponentWithFocus' 中挂钩。
希望这会有所帮助。