繁体   English   中英

TypeError: undefined 不是 object(评估 'route.params.id')

[英]TypeError: undefined is not an object (evaluating 'route.params.id')

我试图在 firebase 中向我的比赛数据库添加比赛条目,但出现错误:

TypeError: undefined 不是 object(评估 'route.params.id')

这是我的代码:


const AddEntry = ({route, navigation}) => {


  const id = route.params.id
  const [title, onChangeTitle] = React.useState('')
  const [image, setImage] = React.useState("/")

  const pickImage = async () => {
      // No permissions request is necessary for launching the image library
      let result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.All,
        allowsEditing: true,
        aspect: [4, 3],
        quality: 1,
      });
  
      console.log(result);
  
      if (!result.cancelled) {
        setImage(result.uri);
      }
    };

  const AddEntry = async() => {
      let imageUrl = await handleImageUpload()
      addEntryToComp({title, imageUrl}, id)
  }
//Image code here

    return(
       <View>
           <Text>Add Entry</Text>
           <Text>{id}</Text>

           <Button title='Upload Entry Image' color="red" onPress={pickImage}></Button>
           <Button title='Save' color="purple" onPress={AddEntry}></Button>
       </View>
    )
}

export default AddEntry

有任何想法吗?

参数用于通过将数据放入 object 作为 navigation.navigate function 的第二个参数来传递数据以进行路由。

例如:

navigation.navigate('AddEntry', { id })

...然后您可以从 route.params 访问那些传递的参数。

更多信息: https://reactnavigation.org/docs/params/

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM