简体   繁体   English

收到的道具在反应本机导航中未定义

[英]Props received are undefined in react native navigation

I am trying to access props passed to a react native component but it seems that props are not received by the component.我正在尝试访问传递给反应本机组件的道具,但该组件似乎没有收到道具。 The component which passes props(PastHires.js):传递 props(PastHires.js) 的组件:

return (
  <ScrollView style={styles.container}>
    <Text style={styles.heading}>Past Hires</Text>
      {
        this.state.boards.filter(item => item.driverId === this.props.user.id && item.hireStatus === 'completed' ).map((item, i) => (
          <ListItem
            style={styles.listItem}
            key={i}
            title={item.pickupDatetime + '  ' + item.hireType.toUpperCase() + '  ' + item.pickupLocation.toUpperCase()}
            leftIcon={{name: 'book', type: 'font-awesome'}}
            onPress={() => {
              this.props.navigation.navigate('PastHireDetails', {
                hireId: 12,
              });
            }}
          />
        ))
      }
  </ScrollView>
);

} }

The component which receives props(PastHireDetails.js):接收道具的组件(PastHireDetails.js):

componentDidMount() {
const { navigation } = this.props;
// console.log(navigation.state)
const ref = Firebase.firestore().collection('hires').doc(JSON.parse(navigation.getParam('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 throws a syntax error: JSON Parse Error: Unexpected identifier "undefined" When I console log navigation.state it returns that params are undefined这会引发语法错误:JSON Parse Error: Unexpected identifier "undefined" When I console log navigation.state 它返回参数未定义

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

Is there anything that I'm doing wrong here?我在这里做错了什么吗? Any fixes?有什么修复吗?

If you used react-navigation, try this for accessing navigation params如果您使用 react-navigation,请尝试使用此方法访问导航参数

 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!");
      }
    }); 
 }

And refer this similar issue https://stackoverflow.com/questions/58279318/navigation-getparam-in-not-an-object/58280498#58280498并参考这个类似的问题https://stackoverflow.com/questions/58279318/navigation-getparam-in-not-an-object/58280498#58280498

this.props.navigation.navigate('PastHireDetails', {
                hireId: 12,
              })

In above line, you have set a navigation parameter called hireId which is an integer在上面的行中,您设置了一个名为hireId 的导航参数,它是一个 integer

const { navigation } = this.props

navigation.getParam('hireId')

navigation.getParam('hireId') will returns you an integer value navigation.getParam('hireId') 将返回一个 integer 值

But in another script you convert that integer to JSON by JSON.parse(navigation.getParam('hireId'))但是在另一个脚本中,您通过JSON.parse(navigation.getParam('hireId'))将 integer 转换为 JSON

this will throw you an error这会给你一个错误

Try remove the JSON.parse as it is a integer already that you want尝试删除 JSON.parse 因为它已经是您想要的 integer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应本机导航(未定义不是对象.. this.props.navigation.navigate - React native navigation (undefined is not an object .. this.props.navigation.navigate React Native - 导航问题“未定义不是对象(this.props.navigation.navigate)” - React Native - navigation issue “undefined is not an object (this.props.navigation.navigate)” undefined 不是一个对象(评估&#39;this.props.navigation&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation') - React Native undefined 不是对象 this.props.navigation.navigate 反应原生 - undefined is not an object this.props.navigation.navigate react native React Native:undefined不是对象(评估&#39;_this2.props.navigation.navigate&#39;) - React Native : undefined is not an object (evaluating'_this2.props.navigation.navigate') 【React-native】undefined不是对象(评估&#39;_this.props.navigation.navigate&#39;) - 【React-native】undefined is not an object (evaluating '_this.props.navigation.navigate') undefined 不是一个对象(评估&#39;this.props.navigation.navigate&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native 未定义(评估&#39;this.props.navigation.navigate&#39;)React Native - undefined (evaluating 'this.props.navigation.navigate') React Native React Native:undefined 不是对象(评估“props.navigation.navigate”) - React Native: undefined is not an object (evaluating 'props.navigation.navigate') TypeError:undefined 不是 React Native 中的对象(评估“_this.props.navigation”) - TypeError: undefined is not an object (evaluating '_this.props.navigation') in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM