通过反应路由器时隐藏 id 或查询字符串 [英] hide id or query string while passing through react router

查看:47
本文介绍了通过反应路由器时隐藏 id 或查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过 id 的路线,但我不想在 url 中显示 id,

i am having route where i pass id,but i dont want to show id in url,

`<Route path={`${match.url}invite-members/:groupID`} exact component={InviteMembers} />`

这在 url 中被转换 https://local..../invite-members/5,但我想要 https://local..../invite-members,但功能应该与我通过 this.props.match.params.groupID 在邀请成员中获得 id 保持不变应该保持原样,请帮助

this gets converted in url https://local..../invite-members/5, but instead of that i want https://local..../invite-members, but the functionality should remain the same as in i get id in invite-members through this.props.match.params.groupID should be as it is,please help

使用反应路由器 "react-router-dom": "^4.2.2",

推荐答案

如果要将 url 更改为 '/invite-members',可以添加 Redirect 组件.如果您想保存 groupId,您可以将其保存到您的组件状态:

If you want to change url to '/invite-members', you can add the Redirect component. And in case you want to save groupId, you could save it to your component state:

import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import {
  Router,
  Route,
  Link,
  Switch,
  Redirect
} from "react-router-dom";

class Root extends PureComponent {
  // add groupId field to your component
  // In case you use redux or any another state management library, you can save groupId to store
  state = { groupId: null };
  render() {
    const { store, history } = this.props;
    // just for example I defined '/' path to redirect on /invite-members url
    return (
        <Router>
          <Switch>
            <Route
              path="/"
              exact
              render={props => (
                <Redirect
                  to={{
                    pathname: "/invite-members/123",
                    state: { from: props.location }
                  }}
                />
              )}
            />
            <Route
              path="/invite-members"
              exact
              render={props => (
                <InviteMembers {...props} groupId={this.state.groupId} />
              )}
            />
            <Route
              path="/invite-members/:groupID"
              exact
              render={props => {
                return (
                  <RedirectAndSaveGroupId
                    {...props}
                    groupId={props.match.params.groupID}
                    onSetGroupId={groupId => {
                      this.setState({ groupId });
                    }}
                  />
                );
              }}
            />
          </Switch>
        </Router>
    );
  }
}

export default Root;

class RedirectAndSaveGroupId extends PureComponent {
  componentDidMount() {
    // save groupId to Root component
    this.props.onSetGroupId(this.props.groupId);
  }

  render() {
    // redirect to /invite-members without groupId
    return (
      <Redirect
        to={{
          pathname: "/invite-members",
          state: { from: this.props.location }
        }}
      />
    );
  }
}

// Just for demo. In this.props.groupId we can receive groupId
class InviteMembers extends PureComponent {
  render() {
    return this.props.groupId;
  }
}

请注意,如果您使用任何状态管理库(例如 Redux),您可以在其中存储组 ID

Note, that in case you using any state management library such as Redux, you can store group id in them

这篇关于通过反应路由器时隐藏 id 或查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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