react-hook-share-state
TypeScript icon, indicating that this package has built-in type declarations

0.0.10 • Public • Published

react-hook-share-state

Share state between different components.

API

useShareState(key:any,initialState?:any):[state,setState]

  • key is used to identify the shared state
  • initialState will not work if the state exist

example :

import {useShareState} from "react-hook-share-state";

const key = 1

function A() {
    const [count, setCount] = useShareState<number>(key,100)
    return <button onClick={() => setCount(a => a - 1)}>{count} -1</button>
}

function B() {
    const [count, setCount] = useShareState<number>(key)
    return <button onClick={() => setCount(a => a + 1)}>{count} +1</button>
}

function App() {
    return  <><B/><A/></>
}

export default App

setShareState(key:any,value:any)

  • assign a value to the state marked by the key

example:

import {setShareState, useShareState} from "react-hook-share-state";

setInterval(()=>setShareState<number>('a',(a=0)=>a+1),1e3)
function App() {
    const [state]=useShareState<number>('a')
    return <h1>{state}</h1>
}

export default App

getShareState(key:any):any

  • get the state marked by the key
import {setShareState, getShareState} from "react-hook-share-state";

setInterval(()=>setShareState<number>('a',(a=0)=>a+1),1e3)
function App() { 
    return <button onClick={()=>alert(getShareState('a'))}>get State</button>
}

export default App

clearShareState(key:any,watch?:boolean)

  • clears the state if no components use it.
  • watch: default false. if true,the state will be auto clear when no components use it.

Package Sidebar

Install

npm i react-hook-share-state

Weekly Downloads

2

Version

0.0.10

License

MIT

Unpacked Size

6.39 kB

Total Files

6

Last publish

Collaborators

  • anytable