Fajrul Aulia
Fajrul Aulia

Reputation: 155

How to Bring Value from State First load to second load in one page

I want bring state from first load to second load, without reset state and without Redux, if I Init state before load, then I setState for change to new Value, then I refresh or I re-call component(second load), without my state reset to init state(like first load), is imposible??

Upvotes: 0

Views: 110

Answers (1)

louie kim
louie kim

Reputation: 509

You can use local storage if you want to use it in the other tabs of the browser too. Use session storage if you just want to store it in the same tab of the browser.

  state = {
    page_value: localStorage.getItem("page_value") || ''
  }

  componentWillUnmount(){
    window.removeEventListener('onbeforeunload', this.compareValue())
  }

  compareValue(){
    const old_page_value = localStorage.getItem("page_value")
    if (this.state.page_value !== old_page_value){
       localStorage.setItem("page_value", this.state.page_value)
     }
  }

Upvotes: 1

Related Questions