反应重新渲染组件,而不是整个页面问题

时间:2018-09-16 04:55:47

标签: reactjs react-router

我正在尝试重新呈现网页上的一个组件以做出反应,但遇到了麻烦。单击后退按钮时,我想转到按字母顺序排列的当前部分之前的部分-将组件的各个部分分解为字母。因此,我的handleBack函数应该重新渲染该组件。这是我的代码如下:

function handleBack() {
    const currentId = window.location.href.split('/').pop();

    const backId = String.fromCharCode(currentId.charCodeAt() - 1);

    return(

       <Route>

         <Redirect from={'/form/${currentId}'} to={'/form/${backId}'} />

       </Route>);

}

例如,它知道currentID是B,而backID是A,但是它没有重新呈现组件并显示页面A。为什么会这样?我该如何解决?我以为路由和重定向可以用,但是什么也没做。

1 个答案:

答案 0 :(得分:0)

第一

将其更改为...。

function handleBack() {
    const currentId = window.location.href.split('/').pop();

    const backId = String.fromCharCode(currentId.charCodeAt() - 1);

    return(

       <Route>

         <Redirect from={'/form/${currentId}'} to={'/form/${backId}'} />

       </Route>);

}

致...

function handleBack() {
    const currentId = this.props.match.params.id; // Use the route params instead of using window.location

    const backId = String.fromCharCode(currentId.charCodeAt() - 1);

    this.props.history.push('/form/${backId}');

}
相关问题