以编程方式更改反应路由器路由参数 [英] Change react router route params programmatically

查看:46
本文介绍了以编程方式更改反应路由器路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单方便的方法可以导航到具有不同参数的同一路线?

Is there any simple and convenient way to navigate to same route with different params?

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

for ex. I'm right now on /profile/idan and I wish to change the param of idan to something else, instantly without dealing with the static part of the route.

推荐答案

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

If your question is about v3 of the API, the react-router library introduces a router context. If you declare that in your contextTypes, you'll have access to a push and replace method. Example:

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;

这篇关于以编程方式更改反应路由器路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆