【问题标题】:I am trying to pass params in stack.screen but having an error我正在尝试在 stack.screen 中传递参数但出现错误
【发布时间】:2021-02-09 17:29:17
【问题描述】:

我的 App.js 中有以下 Stack.Screen,想在另一个屏幕中访问它,但不知道如何操作。

App.js 文件: 使用:{ id: route.params } - 当我使用 console.log(id) 时,它会准确返回 id。现在我想在以下内容中返回相同的 id:<Text>Edit Screen - {id}</Text>

<Stack.Screen name='ShowScreen' 
          component={ShowScreen} 
          options={({route, navigation}) => ({ 
            title: 'Post Details', 
            headerRight: () => (              
              <TouchableOpacity onPress={() => navigation.navigate('Edit', { id: route.params })}>
                <AntDesign name="edit" style={styles.iconStyle} />
              </TouchableOpacity>
            )   
        })} 
        />

EditScreen.js 文件

import React from 'react'
import { View, Text, StyleSheet } from 'react-native'

const EditScreen = ({route}) => {
    const { id } = route.params
    console.log(id)

    return (
        <View>
            <Text>Edit Screen - {}</Text>
        </View>
    )
}

const styles = StyleSheet.create({
    //
})

export default EditScreen

【问题讨论】:

    标签: react-native stack-navigator


    【解决方案1】:

    经过两天的搜索,以下是解决方案: 我只是在 app.js 中的参数之后添加 id 如下 { id: route.params.id }

    <Stack.Screen name='ShowScreen' 
              component={ShowScreen} 
              options={({route, navigation}) => ({ 
                title: 'Post Details', 
                headerRight: () => (
                  <TouchableOpacity onPress={() => navigation.navigate('Edit', { id: route.params.id })}>
                    <AntDesign name="edit" style={styles.iconStyle} />
                  </TouchableOpacity>
                )   
            })} 
            />
    

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 2022-11-13
      • 2022-01-21
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多