React router, pass data when navigating programmatically?

前端 未结 6 1262
广开言路
广开言路 2020-12-07 12:06

We could navigate to different path using

this.props.router.push(\'/some/path\')

Is there a way to send params (object) along when navigating?

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 13:03

    You could make a use of useHistory hook of react-router-dom.

    Below code is for you to pass your data to the stated route which is "/dashboard".

    let history = useHistory();
    
    history.push({
                pathname: '/dashboard',
                state:{
                    tags: 'your-value' 
                }
        });
    

    and from the "/dashboard " you can use the useHistory() to receive the above data.

提交回复
热议问题