react-native - 使用来自 @react-navigation/native 的 StackScreen 选项
问题描述
我需要在屏幕右侧标题上设置一个按钮。
import { useNavigation } from '@react-navigation/native';
const App = () => {
const navigation = useNavigation()
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} options={{headerShown: false}} />
<Stack.Screen name="Next" component={NextScreen} options={{
headerRight: () => <Button title="Done" onPress={() => navigation.navigate('Home')}/>,
headerLeft:null
}} />
<Stack.Screen name="SelectPatient" component={SelectPatientScreen} />
</Stack.Navigator>
</NavigationContainer>
)
}
错误消息显示找不到导航对象。您的组件是否在导航器的屏幕内?
我还在 HomeScreen 中使用导航,它可以工作。App.js 中出现错误。我怎样才能将导航对象放在Press 上。谢谢。
解决方案
只是Guruparan答案的补充,你不能const navigation = useNavigation()
在导航器之外做
推荐阅读
- python - 将多个 PDF 转换为 TXT
- algorithm - 使用多边形 MBR 的 R-Tree 构造算法
- api - Simplygon 云 api 认证
- java - 从 txt.file 填充和删除数据
- c++ - 在 OpenGL 中垂直翻转 FreeType 文本
- sql - 如何在 Pandas 中编写此 SQL?
- wpf - MVVM Light View 不刷新绑定属性
- node.js - npm 或 webpack 错误:找不到模块 'webpack/lib/removeAndDo'
- reactjs - react-toolbox 的 Webpack 4 配置
- node.js - Node.js 工作线程中的 LevelDB