0

For some requirements of my project I would need to display different page types via query parameter (something like):

http;//myweb.com?parameter=value

Is there a way to do that?

EnriqueDev
  • 1,207
  • 2
  • 12
  • 25
  • https://github.com/ReactTraining/react-router/issues/4410#issuecomment-276427066 –  Jan 17 '19 at 16:05
  • Possible duplicate of [How to get parameter value from query string](https://stackoverflow.com/questions/35352638/how-to-get-parameter-value-from-query-string) –  Jan 17 '19 at 16:05
  • not really a duplicated. I want to show different components depending on a query parameter, not redirect. – EnriqueDev Jan 17 '19 at 16:08

2 Answers2

0

Yes this is possible with react-router-dom

Sample Code:

    const queryParams = [];
    for(let i in this.state.someData){
     queryParams.push(encodeURIComponent(i) + "=" + encodeURIComponent(this.state.someData[i])); // this will create a array of property name and value//
    }
    const queryString = queryParams.join("&"); // Will join the Params with & //

       this.props.history.push({
          pathname:'/yourURL',
          search:'?'+ queryString
       });
    }
Praveen Rao Chavan.G
  • 2,772
  • 3
  • 22
  • 33
0

One way to display a diffrent component based on the query parameter, you can use switch in the render :

import * as qs from "query-string";

import ComponentA from "./CmponentA";
import ComponentB from "./CmponentB";

...

render(){
  return(
    {
        (() => {
            const parsed = qs.parse(location.search);

            switch(parsed.parameter){
                case 'a' :
                    return <ComponentA />;
                case 'b' :
                    return <ComponentB />;              
            }
        })();
    }

  );
}
Taki
  • 17,320
  • 4
  • 26
  • 47