1

I got class Component where I'm passing data:

import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class VisitDetails extends Component {
 //...
 render() {
 
 //...
   return() {
      <div>
         {/*pass to function EditVisit.js*/}
        <Link to={{pathname: `/visit/edit/${id}`, state: {data}}}>Edit</Link>
      <div>
   }
 }
}

Now I got function:

import React, { useState } from 'react';
import { useHistory } from "react-router-dom";

function EditVisit (data) {
  console.log(data.info)
 return(
   <div>
    Info: {data.info}
   <div>
 )
}

but data.info is no render (undefined).

2
  • What routing/navigation package are you using in your applicaion/code? Is EditVisit a react component, or really just simply a function that is invoked somewhere? Can you update your question to include how routing works in your app and what is rendering/invoking EditVisit? Commented Dec 1, 2020 at 7:56
  • I have added my imports Commented Dec 1, 2020 at 8:45

3 Answers 3

1

Use the useLocation react hook to access route state.

Given the route

<Link
  to={{
    pathname: `/visit/edit/${id}`,
    state: { data },
  }}
>
  Edit
</Link>

EditVisit

The route state can be accessed from location.state.data.

import React, { useState } from 'react';
import { useHistory, useLocation } from "react-router-dom";

function EditVisit (props) {
  const { state } = useLocation();
  return(
    <div>
     Info: {state.data}
    <div>
  )
}

Why not use useHistory?

History is mutable

The history object is mutable. Therefore it is recommended to access the location from the render props of <Route>, not from history.location. This ensures your assumptions about React are correct in lifecycle hooks.

Sign up to request clarification or add additional context in comments.

4 Comments

one more question: is save then to use history.push(/) ?
@4est I don't quite understand that question.
@4est If your question is about whether or not it is safe to use history.push, yes, that is the correct object to use for imperative navigation.
yes, I want to use for navigation, thanks again
0

If you're using react-router, I think you should use the useHistory hook with location.state to capture the state in React function component. The below is the sample code.

import { useHistory } from 'react-router-dom'
function EditVisit () {
  const history = useHistory()
  const data = history.location.state
 return(
   <div>
    Info: {data.info}
   <div>
 )
}

You can read more at https://levelup.gitconnected.com/how-to-pass-additional-data-while-redirecting-to-different-route-f7bf5f95d48c

3 Comments

when after const data = history.location.state I did console.log I see data data: {user: "07M..3", info: "Test", VisitName: "Test2"...} but into return id did not want to display
{data.data.info} and it's working, thanks for help
@Peter It is better to link official docs as they are likely to be more accurate, but even the blog you linked to doesn't use the history object to access route state. History is mutable and react-router-dom recommends accessing state from the location object, as does the blog you linked out to.
0

in you case you can read it from props

const EditVisit = (props) => {
  const { location: {state: { date }} } = props // date is needed variable from state
  ....
}

screenshot from react-router documentation

Comments

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.