React native 无法从react native中的另一个屏幕获取URL为的参数

React native 无法从react native中的另一个屏幕获取URL为的参数,react-native,React Native,我想将员工ID发送到另一个屏幕。但我无法获取id。它获取默认值id,就像我在输入 默认值1就像我输入默认值5时得到5个数字id一样。但实际上我想让那些员工id看看管理员想要什么。请检查我的代码 renderItem = ({ item}) => { return ( <View style={{flexDirection:'row',}}> <TouchableOpacity style={{height:70}} onPress = {() =>

我想将员工ID发送到另一个屏幕。但我无法获取id。它获取默认值id,就像我在输入 默认值1就像我输入默认值5时得到5个数字id一样。但实际上我想让那些员工id看看管理员想要什么。请检查我的代码

renderItem = ({ item}) => {
return (
  <View style={{flexDirection:'row',}}>
  <TouchableOpacity style={{height:70}} 
    onPress = {() => this.props.navigation.navigate('Employeeeeee' ,  {id : item.id })
    }>   
      <Text style={{ fontSize: 16, marginTop: 5, marginLeft: 15,color:'black',marginLeft:20 }}>
        {item.firstName}
      </Text>         
      <Text style={{ fontSize: 13, marginTop: 5, }}>       
        {item.profEmail}
      </Text>          
    </TouchableOpacity>
    </View> 
  )
 }

when i click on TouchableOpacity. I can't get the id as a parameter on below screen.

  componentDidMount() {   
   const id = this.props.navigation.getParam( 'id', 1) ;
   const url = 'http://104.197.28.169:3000/empProfile/' + id ; 
   fetch(url)
  .then(response => response.json())
  .then((responseJson) => {
    console.log("response " ,responseJson)
    this.setState({
       dataSource: responseJson , 
      isLoading: false
    })
  })
  .catch(error => console.log(error)) 
  }

Here is my Navigation Code.

import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer, createSwitchNavigator} from 'react- 
navigation';
import AppContainer from '../App';
import ViewEmployeeLeave from '../Screens/ViewEmployeeLeave' ; 
import EmployeeProfile from '../Screens/EmployeeProfile' ;

  const eemployeeProfile = createStackNavigator(
  {employeedata  : EmployeeProfile},
  {
  defaultNavigationOptions: ({ navigation }) => ({
  title: 'Employee Profile',
  headerLeft: <TouchableOpacity onPress={() => 
   navigation.navigate('Fourth')}>
     <Image style={{ width: 30, height: 30, marginLeft: 10 }}
      source={require('../icons/icons8-back-arrow-32.png')}></Image>
  </TouchableOpacity>,
  headerTintColor: '#fff',
  headerStyle: {
    backgroundColor: '#008dff',
   },
   })
  },
 );
  export default createAppContainer(
 createSwitchNavigator({    
  AuthLoading: AuthLoadingScreen,
 loginnavigator: AppNavigator,
 Employeeeeee : eemployeeProfile,
  }),
 );
renderItem=({item})=>{
返回(
this.props.navigation.navigate('Employeeeeee',{id:item.id})
}>   
{item.firstName}
{item.profEmail}
)
}
当我点击TouchableOpacity时。我无法在下面的屏幕上获取id作为参数。
componentDidMount(){
const id=this.props.navigation.getParam('id',1);
常量url=http://104.197.28.169:3000/empProfile/“+id;
获取(url)
.then(response=>response.json())
.然后((responseJson)=>{
日志(“响应”,responseJson)
这是我的国家({
数据来源:responseJson,
孤岛加载:false
})
})
.catch(错误=>console.log(错误))
}
这是我的导航码。
从“反应导航堆栈”导入{createStackNavigator};
从“反应”导入{createAppContainer,createSwitchNavigator}
导航';
从“../App”导入AppContainer;
从“../Screens/ViewEmployeeLeave”导入ViewEmployeeLeave;
从“../Screens/EmployeeProfile”导入EmployeeProfile;
const eemployeeProfile=createStackNavigator(
{employeedata:EmployeeProfile},
{
defaultNavigationOptions:({navigation})=>({
标题:“员工档案”,
左校长:
导航。导航('Fourth')}>
,
标题颜色:“#fff”,
头型:{
背景颜色:“#008dff”,
},
})
},
);
导出默认createAppContainer(
createSwitchNavigator({
AuthLoading:AuthLoadingScreen,
登录者:AppNavigator,
雇员:雇员档案,
}),
);

如果您使用的是ReactNavigation v5,您可以从
路由
中分解值,如下所示:

  const { id } = route.params;
请参阅此处的更多信息:

尝试执行此操作

改变

const id = this.props.navigation.getParam( 'id', 1) ;


希望这有帮助

对不起,先生,这个代码对我不起作用。但实际上我得到的是空消息而不是Id,就像我得到的Id与我输入的号码相同,而不是空。请给我一些想法,并再次检查我的代码。由于您正在使用嵌套导航器,您需要从父级获取参数,将编辑答案并检查它。所做的更改现在使用新的检查它。
const id = this.props.navigation.dangerouslyGetParent().getParam('id');