如何使用react-router 4.0刷新当前路由?不重新加载整个页面

Nic*_*.Xu 12 reactjs react-router react-router-v4

'react-router-dom' 在这里具有刷新功能,但是我不知道该如何调用该方法,而且格式正确的文档也不会去解释它。

window.location.reload()对我不起作用,因为它也会清除应用商店。我更改了一些数据,然后需要用更新的数据重新加载路由。

我也读过这篇文章:https : //github.com/ReactTraining/react-router/issues/1982 https://github.com/ReactTraining/react-router/issues/2243

这个线程恰好在讨论我的问题:https : //github.com/ReactTraining/react-router/issues/4056

仍然不知道如何去做。这个基本功能不需要太多的努力,但是在花费了大量的精力之后,我无法重新加载当前路线。

这是我的代码:

@inject('store') @observer
export default class PasswordInput extends Component {
    constructor (props) {
        super(props)
        this.setPwd = this.setPwd.bind(this)
        this.submit = this.submit.bind(this)
    }

    componentWillMount() {
        this.case = this.props.store.case

        this.setState({refresh: false})
    }
    setPwd(e) {
        // console.log(e)
        this.case.pwd = e.target.value

    }
    submit() {
        console.log(this.props.caseId)
        this.setState({refresh: true})

    }

    render() {
        return (
            <Container>
                <div>{this.state.refresh}</div>
                { (this.state.refresh) && <Redirect refresh={true} to={'/cases/' + this.props.caseId}></Redirect>}
                <br />
                <Grid columns="three">
                    <Grid.Row>
                        <Grid.Column key={2}>
                            <Form>
                                <label>Enter Password</label>
                                <input placeholder="empty"  type="text" onChange={this.setPwd} value={this.case.pwd}></input>                               

                                <Button name="Save" color="green" size="mini" 
                                style={{marginTop:'1em'}}
                                onClick={this.submit}>
                                    Submit
                                </Button>                               
                            </Form>
                        </Grid.Column>
                    </Grid.Row>
                </Grid>
            </Container>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

Zac*_*lor 5

我通过将新路由推入历史记录,然后将该路由替换为当前路由(或要刷新的路由)来解决此问题。这将触发react-router在不刷新整个页面的情况下“重新加载”路由。

if (routesEqual && forceRefresh) {
    router.push({ pathname: "/empty" });
    router.replace({ pathname: "/route-to-refresh" });
}
Run Code Online (Sandbox Code Playgroud)

React Router通过使用浏览器历史记录进行导航而无需重新加载整个页面。如果您将路由强加到历史记录中,则React Router将检测到此情况并重新加载路由。更换空路径很重要,这样在推入后您的后退按钮不会将您带到空路径。

根据react-router的说法,react路由器库似乎不支持此功能,并且可能永远不会支持此功能,因此您必须以hacky的方式强制进行刷新。


Alb*_*ras 5

你可以使用这个小技巧。在历史记录中推送一个新路径,例如history.push('/temp')并立即调用history.goBack()

这里是例子:

创建历史记录并将其传递给路由器:

import { createBrowserHistory } from "history";

export const appHistory = createBrowserHistory();

<Router history={appHistory}>
  <Route exact path="/" component={Home} />
  <Route path="/about" component={About} />
</Router>
Run Code Online (Sandbox Code Playgroud)

然后在您的应用程序中的某个位置更改历史记录:

appHistory.push('/temp');
appHistory.goBack();
Run Code Online (Sandbox Code Playgroud)

这样,路线将“保持”相同,并且将被刷新。


Kwu*_*ung 2

我也有同样的问题,不知道刷新是怎么做的。我的解决方案是执行以下两个步骤。

  1. 推动历史新道路
  2. 重置所有状态

然后 DOM 将根据您的路线重新渲染。