反应路由器重定向-将参数添加到路径名

时间:2020-05-01 22:32:56

标签: javascript reactjs redirect react-router react-router-dom

我使用React Router Redirect,我有一个参数newId,它是一些数字,我想将其添加到url中。例如,如果“ newId”为10,我想转到“ / admin / add-questions / 10”。

类似于下面的内容,但是:${newId}不起作用。它将我重定向到“ admin / add-questions /:$%7BnewId%7D”。我也尝试了:newId和其他解决方案,但找不到解决方法:(

render() {

    const newId = 10;

     return (
        <Redirect
          to={{ 
            pathname: '/admin/add-questions/:${newId}',
            state: { key: 'value' } }}
        />
      );
}

1 个答案:

答案 0 :(得分:3)

请考虑使用反引号替换您的单引号,以使模板文字正常工作。

pathname: `/admin/add-questions/${newId}`,