最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react router is only replacing the final route on history.push - Stack Overflow

programmeradmin1浏览0评论

I am trying to redirect with react router to another page after a certain amount of time has passed.

The code I have so far is:

 submitActivity(){
        axios.post('/tiles', {
            activityDate:this.state.startDate,
            planId:this.state.planId,
            value:this.state.sliderValue
        })
            .then(res=>{
                console.log(res);
                this.modalHandleShow();
                setTimeout(function(){
                    this.goBackToTile();
                }.bind(this),3000);
            })
            .catch(err=>console.log(err));
    }

    goBackToTile(){
        this.props.history.push(`tile/${this.state.tileId}`)
    }

history is definitely being called, but the url which is currently

/addActivity/tile/2/plan/9

only gets changed to /addActivity/tile/2/plan/tile/2

while /tile/2 is correct I don't understand why the rest of the url stays in tact?

I am trying to redirect with react router to another page after a certain amount of time has passed.

The code I have so far is:

 submitActivity(){
        axios.post('/tiles', {
            activityDate:this.state.startDate,
            planId:this.state.planId,
            value:this.state.sliderValue
        })
            .then(res=>{
                console.log(res);
                this.modalHandleShow();
                setTimeout(function(){
                    this.goBackToTile();
                }.bind(this),3000);
            })
            .catch(err=>console.log(err));
    }

    goBackToTile(){
        this.props.history.push(`tile/${this.state.tileId}`)
    }

history is definitely being called, but the url which is currently

/addActivity/tile/2/plan/9

only gets changed to /addActivity/tile/2/plan/tile/2

while /tile/2 is correct I don't understand why the rest of the url stays in tact?

Share Improve this question asked Jul 10, 2018 at 23:10 user4747724user4747724 0
Add a ment  | 

1 Answer 1

Reset to default 16

Make sure you include a / in the beginning of the string, or it will be used relative to your current url.

goBackToTile() {
    this.props.history.push(`/tile/${this.state.tileId}`)
}
发布评论

评论列表(0)

  1. 暂无评论