Javascript React Native Navigation,如何在通过Navigation导航到路由时刷新路由。navigate(“routename”,{randomparams})
我有一个底部导航器,它有两个堆栈导航器。每个stacknavigator都有各自的屏幕。每当我使用类似Javascript React Native Navigation,如何在通过Navigation导航到路由时刷新路由。navigate(“routename”,{randomparams}),javascript,react-native,react-native-navigation,Javascript,React Native,React Native Navigation,我有一个底部导航器,它有两个堆栈导航器。每个stacknavigator都有各自的屏幕。每当我使用类似 navigator.navigate("Stackname" {screen:"screenname", randomProp: "seomthing") 参数被发送到stacknavigator,而不是屏幕本身。我通过路过这里,有点忽略了这个问题 initialParams=route.params 在stacknavigators中,但当我第二次调用第一块代码时,它们不会刷新。 有什么
navigator.navigate("Stackname" {screen:"screenname", randomProp: "seomthing")
参数被发送到stacknavigator,而不是屏幕本身。我通过路过这里,有点忽略了这个问题
initialParams=route.params
在stacknavigators中,但当我第二次调用第一块代码时,它们不会刷新。
有什么想法吗 这是因为屏幕已安装&初始参数不会更新。不过,您可以创建一个包装器组件,该组件使用“react native navigation”提供的“withNavigationFocus”增强 焦点组件
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>
}
navigator.navigate("Stackname" {screen:"screenname", randomProp: "seomthing")
您可以改为执行以下操作:
navigator.navigate("screenName", {'paramPropKey': 'paramPropValue'})
这将起作用,因为“.navigate”函数会找到与名称匹配的第一个可用屏幕,如果尚未装入,则会将其装入堆栈(激发componentDidMount方法)。如果屏幕已经存在,它只是导航到它,忽略“componentDidMount”,但传递“isFocused”道具,幸运的是,我们在“ComponentWithFocus”中连接到了该道具
希望这有帮助