1

On click of Link am passing query string parameter and am reading it on another page then am doing my logic. but I want to hide the query string from URL. this is how am passing parameters

<Link  to={{ pathname: '/user', query: { id: this.props.id } }} > User List </Link>
8
  • this is how am passing params User List <Link to={{ pathname: '/details', query: { id: this.props.id } }} > More Details... </Link> Commented Feb 7, 2017 at 12:16
  • 1
    Basically you cannot hide the query strings from the url.You can store somewhere to use it but url the only option where you can pass query parameters to receive in another page. Commented Feb 7, 2017 at 12:41
  • for example if i the query string will appear like this at URL xxxxx ?id=1001 then some one can hit 1002 can check details of 1002 's also right . how to protect Commented Feb 7, 2017 at 12:47
  • Then do one thing.Mix the id with some random characters which u know how to mix and that cannot be easily extracted.So it is like adding a hash in password.You can do that if you want that much security.Otherwise different tabs cannot show different content if you save that id some where else. Commented Feb 7, 2017 at 12:52
  • If the goal is to keep the query parameters private then how is adding random characters on the front end going to achieve that when anyone can see what these random characters are? Commented Feb 7, 2017 at 12:55

1 Answer 1

1

Programmatically navigate in react-router whilst hiding query string parameters

Note the second argument takes an object of parameters. Use the following inside your component:

this.context.transitionTo("route", { search: this.state.search, type: this.state.type });

We need to use the class based component syntax to have access to the this keyword.For example:

import React from 'react';

export default class CustomLink extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(e) {
    e.preventDefault();

    this.context.router.transitionTo("route", { search: this.state.search, type: this.state.type });
  }

  render(){
    return (<div onClick={this.handleClick}>Click</div>);
  }
}

customLink.contextTypes = {
  router: React.PropTypes.func.isRequired
};
Sign up to request clarification or add additional context in comments.

5 Comments

i imported already browser history package like this . import { Router, Route, browserHistory} from 'react-router';
Should i give inside <Link to= ""> </Link> or render function of class/component
Again, see updated answer. Link is a react-router component and therefore wont have this custom functionality.
Uncaught TypeError: Cannot read property 'transitionTo' of undefined
Ensure that the name of the component is accurate when setting contextTypes. i.e if your component is called customLink then use customLink.contextTypes = ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.