【问题标题】:Change react router route params programmatically以编程方式更改反应路由器路由参数
【发布时间】:2017-08-25 03:14:46
【问题描述】:

有没有简单方便的方法来导航到不同参数的同一条路线?

例如。我现在在 /profile/idan 上,我希望立即将 idan 的参数更改为其他参数,而无需处理路由的静态部分。

【问题讨论】:

  • 这是 v3 还是 v4?

标签: javascript reactjs react-router


【解决方案1】:

如果您的问题是关于 API 的 v3,react-router 库引入了 router 上下文。如果您在contextTypes 中声明,您将可以访问pushreplace 方法。示例:

import React, { Component, PropTypes } from 'react';
import { PropTypes as RouterPropTypes } from 'react-router';

class MyComponent extends Component {
  // ...
  handleSomeEvent() {
    this.context.router.push('/some/url');
  }
}

MyComponent.contextTypes = {
  router: RouterPropTypes.routerShape
};

export default MyComponent;

【讨论】:

  • 它是 v3,实际上push 是我不喜欢使用的,因为我无法切换参数,我必须指定整个路线,我的主要问题是如何避免它。还是谢谢。
  • 我明白了。他们明确删除了对此类事情的支持。您可能想查看github.com/ReactTraining/react-router/issues/1840 的上下文。本质上,解决方案是“构建你自己的”,并使用低级推送机制。没有构建路由 URL 的功能。
猜你喜欢
  • 2017-09-03
  • 2015-08-30
  • 1970-01-01
  • 2017-09-27
  • 2018-12-28
  • 2017-06-14
  • 2018-03-11
  • 2020-10-02
相关资源
最近更新 更多