2

I used this Link , when i refresh the page props.location.state === null any solution to keep the data in props.location.state when refreshing or charging new pages without using local storage?

<Link to={{ pathname: `/details/${imdbID}`, state: this.props }}>
B--rian
  • 5,578
  • 10
  • 38
  • 89
Maria-Elena
  • 127
  • 1
  • 11
  • 2
    If you want to persist the state across refresh, you need to store it in localStorage and retrieve it when the component is mounted instead of passing it though state with Link – Shubham Khatri Jan 28 '19 at 09:36

2 Answers2

0

You have to store the state you wish to save into local storage.

let savedState =  JSON.stringify(props.location.state)
localStorage.setItem('myState', savedState);

To get the saved state from local storage

let retrievedState = JSON.parse(localStorage.getItem('myState'));
Muljayan
  • 3,588
  • 10
  • 30
  • 54
0

No need to store the data you need in local storage, you can dump in any place and with or without refreshing the data will be always here just in routing we need to use propWhatever example:

<Route path="/details" render={(props)=> (
        
          <Details {...props}   propWhatever={{data}} />
          )} /> 
Maria-Elena
  • 127
  • 1
  • 11