react 动态修改路由_如何使用react-router-dom创建动态路由?

我学会了反应,知道如何创建静态路由,但无法弄清楚动态路由。也许有人可以解释,我将不胜感激。假设有两个组件,一个用于渲染路线,另一个作为路线的模板。也许代码有问题,但希望您理解..

这是呈现路线的组件:

import React, { Component } from 'react';

import axios from 'axios';

import Hero from './Hero';

class Heroes extends Component {

constructor(props) {

super(props);

this.state = {

heroes: [],

loading: true,

error: false,

};

}

componentDidMount() {

axios.get('http://localhost:5555/heroes')

.then(res => {

const heroes = res.data;

this.setState({ heroes, loading: false });

})

.catch(err => { // log request error and prevent access to undefined state

this.setState({ loading: false, error: true });

console.error(err);

})

}

render() {

if (this.state.loading) {

return (

Loading...

)

}

if (this.state.error || !this.state.heroes) {

return (

An error occured

)

}

return (

//what should be here?

);

}

}

export default Heroes;

请求的JSON如下所示:

const heroes = [

{

"id": 0,

"name": "John Smith",

"speciality": "Wizard"

},

{

"id": 1,

"name": "Crag Hack",

"speciality": "Viking"

},

{

"id": 2,

"name": "Silvio",

"speciality": "Warrior"

}

];

路线组件(也许应该有道具,但是如何以正确的方式做到):

import React, { Component } from 'react';

class Hero extends Component {

render() {

return (

//what should be here?

);

}

}

export default Hero;

我在浏览器中需要这样的东西,并且每个路由URL的ID都应该有所不同(heroes / 1,heroes / 2 …):

约翰·史密斯(John Smith)Crag Hack Silvio

他们每个人:

约翰·史密斯。 向导

等等…

非常感谢您的帮助!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值