【问题标题】:React history push updating url but not navigating反应历史推送更新网址但不导航
【发布时间】:2021-11-14 13:38:39
【问题描述】:

以下问题:

const history = createBrowserHistory()
render() {
    return (
      <Router history={history}>
          <WButton history={history}/>
          <Switch>
               <Route exact path='/' component={Home}/>
               <Route exact path='/next' component={Next}/>

当我在 WButton 组件中执行 this.props.history.push("/next") 时,它只会更新 url 而不会切换到下一个组件。当我将 WButton 移到 Home 组件中时,一切正常,但我想将 WButton 保持在顶层。这有可能吗?

createBrowserHistory({ forceRefresh: true }) 也可以在顶层使用,但我不想在每个导航步骤中重新加载页面。

【问题讨论】:

    标签: reactjs url react-router react-router-dom history


    【解决方案1】:

    找到解决方案:

    我可以在 WButton 组件中使用 export default withRouter(WButton) 而不是标准导出,然后通过道具删除手动注入历史记录。

    导入是import { withRouter } from 'react-router-dom';

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2021-08-06
      • 2018-01-02
      • 2017-06-29
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 2021-04-03
      • 2020-12-26
      相关资源
      最近更新 更多