React Native:出现“未定义路由键”错误

3

我的代码位于这里

我收到一个错误消息,说: 没有为key employeeList定义路由。必须是以下之一:'auth','main'。我已经检查了router.js,这个组件是嵌套在主场景中的,所以我不确定问题出在哪里。当我想要创建或删除员工时,我会遇到这个问题。我认为这与返回employeelist屏幕有关。

enter image description here

1个回答

2

<Link> 更改为 <TouchableOpacity>,并从 react-native-router-flux 导入 stack,并对根目录和认证进行相同操作。

当您转到堆栈的第一个元素时,需要使用其键调用它。这意味着如果您想转到 employeeList,则必须调用 Actions.main()。

这就是为什么人们通常使用屏幕名称命名其父堆栈,并以 "_" 前缀命名第一个场景。您可以使用 employeeList 命名堆栈,并使用 _employeeList 命名第一个场景。请尝试此操作并告诉我结果。

<Router navigationBarStyle={{ backgroundColor: '#2980b9'}}  titleStyle={{ color:'#fff', alignSelf:'center' }} >
        <Stack key="root" hideNavBar={true}>
            <Stack key='auth'>
                <Scene key='login' component={LoginForm} title = 'Please login' />
            </Stack>
            <Stack key='employeeList'>
                <Scene
                    key='_employeeList'
                    component={EmployeeList}
                    title ='Employee'
                    rightTitle="Add"
                    onRight={()=> Actions.createEmployee()} 
                    initial 
                    rightButtonTextStyle ={{ color:'white',alignSelf:'center',marginRight:5 }}
                /> 
                <Scene
                    key='createEmployee'
                    component={EmployeeCreate}
                    title='Create Employee'            
                />     
            </Stack>
        </Stack>
    </Router>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接