I am passing the props to a functional component via Link (react-router-dom) but it says the state is 'undefined'

Syed Kamran Ahmed

I need to pass the id to a different component.

import { Link } from "react-router-dom";

const Option = () => {
  return (
    <section className="section">
      <Link
        to={{
          pathname: "/person",
          state: { id: "12345" },
        }}
      >
        <button class="button is-primary">Click Me</button>
      </Link>
    </section>
  );
};
export default Option;

The component where I am accessing the id

const Person = (props) => {
  let data = useLocation();
  console.log(data);
  return (
    <section className="section has-text-centered mt-4">
      <h1>Hello</h1>
    </section>
  );
};

export default Person;

But I am getting state as undefined. How do I access the id that I have passed in ?

State is undefined

prasanth

You need to assign the passing props on route component assigned place not ah route navigation

Reference Example for passing props to router

Or else if you really do like passing props on onclick. use useHistory hook then pass the state param

you could receive the state value via props in child component

Updated

you should pass the render params to the component like

<Route path="/person" render={Person} />

Is equal to

<Route path="/person" render={(params)=><Person {...params}/>} />

Then only you can receive the props value on child param

CodeSandBox

import { Link,useHistory } from "react-router-dom";
    
    const Option = () => {
      const history = useHistory();
      function nav(){
         history.push({
          pathname: '/path',
          state: {  // location state
            update: true, 
          },
        });
      } 
      return (
        <section className="section">
            <button class="button is-primary" onClick={nav}>Click Me</button>
        </section>
      );
    };
    export default Option;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am passing the state to a function component via history (react-router-dom) but it says the state is 'null'

React Router <Link> and <Route> not passing state to Component

React Router 4 - passing state in Link component

How can I pass props to a Link component from react-router-dom via Semantic UI's augmentation?

Passing Props in React Functional Component

react-redux state undefined of props - functional component

How, using react-router-dom, can I direct a link to render a general post component, populated with props?

Passing react-router-dom Link component as a render prop is failing

React router dom passing data from parent component to child router component does not pass the props.match

Why is state undefined when passed through React Router Link Component?

In React router dom can i pass a props in a Link?

Reducer passing state as props in a component undefined

Passing React Router Link "to" parameter as props

passing props with Link in React-Router

Passing Props to Child Component as State in React

Cant passing props via React Router

React not passing state to child via props

Styling react-router-dom Link using styled-components getting warning when passing props

React Router not rerendering same <Link> component on props

React Router - Passing match via props to a component rendered using Route::render

Getting toogleShow is not function when passing as props in react typescript functional component

Passing props to router component in react-router v4

React: Changing State in functional component also Change functional component's props value, and its Parent Class State

React Router Dom Link in another component

React - I am passing state from a child to a parent component but the state value is off by one update cycle

Cannot render Component passing React state to props for another Component

Set state from props change in react functional component?

React - undefined props error when rendering functional component

I'm getting TypeError: Cannot set property 'props' of undefined while trying to use props inside functional component in React?

TOP Ranking

HotTag

Archive