1

I use React with React Router and I have 3 links which has same path as component. How can I pass different params on click to component?

const Example = props => {
  console.log("props", props);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
};

function App() {
  return (
    <BrowserRouter>
      <Switch>
        <Route path={"/example"} render={() => <Example />} />
      </Switch>
      <>
        <Link to={"/example"} params={{ name: "john" }}>
          <button>name</button>
        </Link>
        <Link to={"/example"} params={{ age: "27" }}>
          <button>age</button>
        </Link>
        <Link to={"/example"} params={{ surname: "Travolta" }}>
          <button>surname</button>
        </Link>
      </>
    </BrowserRouter>
  );
}

CodeSanbox

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54

2 Answers2

0

Your improved code

const Example = props => {
  console.log("props", props);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Link to={{ pathname: "/Example2", state: { name: "Hello world..." } }}>
        Navigate to Example2
      </Link>
    </div>
  );
};
const Example2 = props => <h1>Hey...{props.location.state.name}</h1>;
function App() {
  return (
    <BrowserRouter>
      <Switch>
        <Route path={"/example"} render={() => <Example />} />
        <Route path={"/Example2"} render={Example2} />
      </Switch>
      <>
        <Link to={"/example"} params={{ name: "john" }}>
          <button>name</button>
        </Link>
        <Link to={"/example"} params={{ age: "27" }}>
          <button>age</button>
        </Link>
        <Link to={"/example"} params={{ surname: "Travolta" }}>
          <button>surname</button>
        </Link>
      </>
    </BrowserRouter>
  );
}

This is how can pass properties through Link

   <Link to={{ pathname: "/Example2", state: { name: "Hello world..." } }}>
       Navigate to Example2
   </Link>

and access like this in navigated component if it's functional component

props.location.state.name

if class based component

this.props.location.state.name
akhtarvahid
  • 9,445
  • 2
  • 26
  • 29
0

You can use the to property of Link, and access the state in Example component with props.location.state.

Codesandbox

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
import "./styles.css";

const Example = props => {
  console.log("props", props.location.state);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
};

function App() {
  return (
    <BrowserRouter>
      <Switch>
        <Route path="/example" component={Example} />
      </Switch>
      <>
        <Link to={{pathname:"/example", state: {name: "john"}}} >
          <button>name</button>
        </Link>
        <Link to={{pathname:"/example", state: {age: "27"}}} >
          <button>age</button>
        </Link>
        <Link to={{pathname:"/example", state: {surname: "Travolta"}}}  >
          <button>surname</button>
        </Link>
      </>
    </BrowserRouter>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54