最近,我已经开始使用React Native 0.61.5
和React导航5.x
。现在,我需要将父函数传递给子屏幕。例如,我想通过按嵌套在StackNavigator中的TabNavigator标签中的按钮来更改导航栏图标。以前(导航版本3.x)我使用getActiveChildNavigationOptions
来满足需要,但现在将其删除。因此,实现此目标的唯一方法是将自定义函数传递给子组件。
在示例代码中,我想从XXXXXXXX
更改为YYYYYYYYY
:
const TabA = ({ route }) => (
<View>
<Text>Tab A</Text>
<Button
title="Change header"
onPress={() => route.params.setHeaderRightName("YYYYYYYYY")}/>
</View>
)
const TabB = () => <><Text>Tab B</Text></>
const Tabs = createBottomTabNavigator();
const TabsNavigator = ({ navigation }) => {
const [headerRightName, setHeaderRightName] = useState("XXXXXXXX");
useLayoutEffect(() => navigation.setOptions({
headerRight: () => <MyNavIcon name={headerRightName}/>
}), [headerRightName]);
return (
<Tabs.Navigator>
<Tabs.Screen name="tab-a" component={TabA} initialParams={{ setHeaderRightName }} />
<Tabs.Screen name="tab-b" component={TabB} />
</Tabs.Navigator>
);
}
const Stack = createStackNavigator();
export default App = () => (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="tabs" component={TabsNavigator} />
<Stack.Screen name="another-screen" component={AnotherScreen} />
</Stack.Navigator>
</NavigationContainer>
)
一切正常,但我收到此警告:
全文:
Non-serializable values were found in the navigation state, which can break usage such as persisting and restoring state. This might happen if you passed non-serializable values such as function, class instances etc. in params. If you need to use components with callbacks in your options, you can use 'navigation.setOptions' instead. See https://reactnavigation.org/docs/troubleshooting#i-get-the-warning-non-serializable-values-were-found-in-the-navigation-state for more details.
因此,如果警告说我无法使用路由参数传递功能,如何将功能从父母传递给孩子?
警告中的链接所指向的文档页面说:
如果您不使用状态持久性或不使用接受参数中功能的屏幕的深层链接,则可以忽略该警告。要忽略它,可以使用YellowBox.ignoreWarnings。
因此,就我而言,我可以忽略此警告并以安全的方式传递函数,以考虑传递参数。
import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings([
'Non-serializable values were found in the navigation state',
]);