使用url-params将值从一条路径传递到另一条路径react router v4

时间:2018-10-12 19:06:38

标签: javascript reactjs react-router

//value push from /status -> /schedule/:selfUri
    getSchedule = e => {
    e.preventDefault();
        this.props.history.push(`/schedule/${this.state.selfUri}`)
    }

// router-container
<Router>
    <Route path="/status" component={Status} exact />
    <Route path="/schedule/:selfUri" component={Schedule}/>
</Router>

// link-container
<Link to="/schedule/selfUri">Schedule</Link>

//Schedule component
class Schedule extends React.Component {
    componentDidMount() {
        const { selfUri } = this.props.match.params;
        console.log(selfUri)
        axios({
            method: 'get',
            url: `${selfUri}`

与其尝试将值推入/schedule,而是尝试加载网址/schedule//value/of/selfUri   我只想在/schedule获得selfUri的值。

  //Using localStorage I can do          
getSchedule = e => {
      e.preventDefault();
      localStorage.setItem('selfUri', this.state.selfUri);
      this.props.history.push(`/schedule`)
      // this.props.history.push(`/schedule/${this.state.selfUri}`)

}


  //Then from /schedule path
   componentDidMount() {
        // const { match: { params: { selfUri }}} = this.props;
        // console.log(this.props.match.params)
        var selfUri = localStorage.getItem('selfUri')
        console.log(selfUri)
        axios({
            method: 'get',
            url: `${selfUri}`,
        })

0 个答案:

没有答案