npm install --save sessionstate
This simple hook should make it easier to add JSON and string objects to the window sessionStorage variable using useState.
// string
import React, { useEffect } from 'react'
import { useSessionState } from 'sessionstate'
const Example = () => {
const [state, setState] = useSessionState("window.sessionStorage variable")
useEffect(()=>{
console.log("sessionStorage: " + window.sessionStorage["window.sessionStorage variable"])
console.log("sessionState: " + state)
}, [state])
return (
<div>
<p>{state}</p>
<button onClick={()=>setState("Your value")}>Save value</button>
</div>
)
}
// JSON
import React, { useEffect } from 'react'
import { useSessionState } from 'sessionstate'
const Example = () => {
const [state, setState] = useSessionState("window.sessionStorage variable")
useEffect(()=>{
console.log("sessionStorage: " + window.sessionStorage["window.sessionStorage variable"])
console.log("sessionState: " + state)
}, [state])
return (
<div>
<p>{state}</p>
<button onClick={()=>setState([{"key1": "value1"}, {"key2": "value2"}])}>Save value</button>
</div>
)
}
If you find a bug or a flaw in the code, please email me: id_1.0@mail.ru.
Thanks you!
MIT © BEISER901
This hook is created using create-react-hook.