ReactRouter:无法读取属性“imageId"未定义的 [英] ReactRouter: Can not read property "imageId" of undefined

查看:57
本文介绍了ReactRouter:无法读取属性“imageId"未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 React Router 用于 react-native 项目.在我的 index.js 文件中,我有:

I am trying to use React Router for a react-native project. In my index.js file I have:

  <NativeRouter>
    <View>
      <Route path="/" component={MainComponent} />
      <Route path="/images/:imageId/" component={ShowImage} />
    </View>
  </NativeRouter>

在我的 DisplayComponent 中:

And in the DisplayComponent I have:

    <View}>
      {items.map( item => {
        return (
          <Link to="images/7326" key={item.id}>
            <Image
              source={{uri: 'https://someImageLink...'}}
            />
          </Link>
        )
      })}
    </View>

并显示图像如下所示:

export default class ShowImage extends Component {
  render() {
    return (
      <View>
        <Text>{this.props.params.imageId}</Text>
     </View>
   );
 }
}

当我点击加载的图片时,出现以下错误:

When I click on a loaded image I get the following error:

can not read "imageId" of undefined. 

所以我猜道具没有被传递,但无法弄清楚在哪里......感谢所有的帮助.

So I am guessing the props arent being passed in but cant figure out where... Thanks for all the help.

推荐答案

在最新版本的 react-router 中,我猜你正在使用,路由参数可以从 match 道具.所以为了得到imageId

In the latest version of react-router, which I am guessing you are using, the route params are available from the match prop. So in order to get the imageId

使用

  <Text>{this.props.match.params.imageId}</Text>

这篇关于ReactRouter:无法读取属性“imageId"未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆