Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2. import { withRouter } from 'react-router-dom';
  3.  
  4. // const BrowserRouterComponent = <BrowserRouter>
  5. //     <Switch>
  6. //         <Route path="/AdminPanel" component={AdminPanel} />
  7. //         <Route path="/user" component={UserPanel} />
  8. //         <Route exact path="/" component={App} />
  9. //         <Route exact path="/example" component={Example} />
  10. //         <Route path="/404" component={error404} />
  11. //         <Redirect to="/404" />
  12. //     </Switch>
  13. // </BrowserRouter>;
  14.  
  15. class Comp extends React.Component<any>{
  16.  
  17.     public render() {
  18.         return (
  19.             <div>
  20.                 <h1>id: {this.props.id}</h1>
  21.                 <h1>old: {this.props.old}</h1>
  22.                 <h1>search:{this.props.search}</h1>
  23.                 <h1>key1:{this.props.key1}</h1>
  24.                 <h1>key2:{this.props.key2}</h1>
  25.                 <h1>key3:{this.props.key3}</h1>
  26.  
  27.             </div>
  28.         );
  29.     }
  30. }
  31.  
  32.  
  33. class Example extends React.Component<any>{
  34.  
  35.      queryStringToObject = (queryString:any) => {
  36.         let pairs = queryString.slice(1).split('&');
  37.         const result: any = {};
  38.         pairs.forEach((pair: string) => {
  39.             const pairA: string[] = pair.split('=');
  40.             result[pairA[0]] = decodeURIComponent(pairA[1] || '');
  41.         });
  42.         return result;
  43.     }
  44.  
  45.     public render() {
  46.         let pathsProps;
  47.  
  48.         if(this.props.location.search !== ''){
  49.             console.log(this.props.location.search);
  50.             pathsProps = this.queryStringToObject(this.props.location.search);
  51.             console.log('myObjest',pathsProps);
  52.         }else{
  53.              pathsProps = this.props.match.params;
  54.             console.log(this.props.match.params);
  55.         }
  56.         return (
  57.            <Comp {...pathsProps}/>
  58.         );
  59.     }
  60. }
  61. export default withRouter(Example);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement