Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 收到的道具在react本机导航中未定义_Javascript_React Native_Expo_React Native Navigation - Fatal编程技术网

Javascript 收到的道具在react本机导航中未定义

Javascript 收到的道具在react本机导航中未定义,javascript,react-native,expo,react-native-navigation,Javascript,React Native,Expo,React Native Navigation,我试图访问传递给react本机组件的道具,但组件似乎没有接收到道具。 传递道具的组件(PastHires.js): } 这会引发语法错误:JSON解析错误:意外标识符“未定义” 当我输入console log navigation.state时,它返回未定义的参数 Object { "key": "PastHireDetails", "params": undefined, "routeName": "PastHireDetails", } 这里有什么我做错的吗?有任何修正吗?如果

我试图访问传递给react本机组件的道具,但组件似乎没有接收到道具。 传递道具的组件(PastHires.js):

}

这会引发语法错误:JSON解析错误:意外标识符“未定义” 当我输入console log navigation.state时,它返回未定义的参数

Object {
  "key": "PastHireDetails",
  "params": undefined,
  "routeName": "PastHireDetails",
}

这里有什么我做错的吗?有任何修正吗?

如果您使用了react导航,请尝试使用此选项访问导航参数

 componentDidMount() {
    const { navigation } = this.props;
    // console.log(navigation.state);
    const hireId = navigation.state.params.hireId;
    const ref = Firebase.firestore().collection('hires').doc(hireId);
    ref.get().then((doc) => {
      if (doc.exists) {
        this.setState({
          hire: doc.data(),
          key: doc.id,
          isLoading: false
        });
      } else {
        console.log("No such document!");
      }
    }); 
 }
并参考类似问题

在上面的一行中,您设置了一个名为hireId的导航参数,它是一个整数

const { navigation } = this.props

navigation.getParam('hireId')
getParam('hireId')将返回一个整数值

但在另一个脚本中,您可以通过
JSON.parse(navigation.getParam('hireId'))

这会给你一个错误


尝试删除JSON.parse,因为它已经是您想要的整数

检查PastHires.js是否可以在导航器中看到PasthiredDetails.js,如导出默认stackNavigator=createStackNavigator({PastHires,PasthiredDetails},@mmelotti pastHires.js中应该有堆栈导航器吗?我所有的导航屏幕都在堆栈导航器中
 componentDidMount() {
    const { navigation } = this.props;
    // console.log(navigation.state);
    const hireId = navigation.state.params.hireId;
    const ref = Firebase.firestore().collection('hires').doc(hireId);
    ref.get().then((doc) => {
      if (doc.exists) {
        this.setState({
          hire: doc.data(),
          key: doc.id,
          isLoading: false
        });
      } else {
        console.log("No such document!");
      }
    }); 
 }
this.props.navigation.navigate('PastHireDetails', {
                hireId: 12,
              })
const { navigation } = this.props

navigation.getParam('hireId')