【问题标题】:React browserhistory.push not working for the first time反应 browserhistory.push 第一次不起作用
【发布时间】:2019-02-21 23:45:03
【问题描述】:

我正在使用 url 参数加载页面,然后使用 browserhistroy.push() 重置为根 url,第一次它不起作用,当我看到日志时,props.param 显示相同的旧 url 路径名。第二次我再次点击它按预期工作。 注意:将 React 与 redux 一起使用,使用“react-router”。 代码: 路由.js

{
      path: '/:menuitem',
      name: 'Products',
      getComponent(nextState, cb) {
        const importModules = Promise.all([
          import('views/Products/modules/reducer'),
          import('views/Products/modules/sagas'),
          import('views/Products'),
        ]);

        const renderRoute = loadModule(cb);

        importModules.then(([reducer, sagas, component]) => {
          injectReducer('search', reducer.default);
          injectSagas(sagas.default);
          renderRoute(component);
        });
        importModules.catch(errorLoading);
      },
    },

在组件中:设置参数/url

 var urlname = options[secIndex][itemIndex].name;
      urlname = urlname.replace(/\s+/g, '-').toLowerCase();
      console.log(urlname);
      browserHistory.push('/'+urlname);
      this.props.leftStripActions.onFilterByLens(options[secIndex][itemIndex]);

重置为root: browserHistory.push('/'); [第一次不行enter code here]

任何帮助将不胜感激!!!

【问题讨论】:

    标签: reactjs redux react-router


    【解决方案1】:
    use => this.props.history.push(url)
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 2020-06-25
    • 1970-01-01
    • 2020-04-16
    • 2018-10-25
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多