我正在尝试将React Router用于本机项目。在我的index.js文件中,我有:
<NativeRouter> <View> <Route path="/" component={MainComponent} /> <Route path="/images/:imageId/" component={ShowImage} /> </View> </NativeRouter>
在DisplayComponent中,我有:
<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> ); } }
单击加载的图像时,出现以下错误:
can not read "imageId" of undefined.
所以我想道具传入了,但无法弄清楚……谢谢您的所有帮助。
react-router我猜您正在使用的最新版本的中,match道具可以提供路线参数。所以为了得到imageId
react-router
match
imageId
用
<Text>{this.props.match.params.imageId}</Text>