Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

supun-io/preact-global-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a simple version (79 lines, 200 bytes minified gziped) of React state-pool. This only contains one Preact hook, useGlobalState that can be used to set/get global states.

Installing

npm install preact-global-state

Usage

import { store, useGlobalState } from 'preact-global-state';

store.init({
    name: "Ghost"
});

function App() {
    const [name, setName] = useGlobalState('name');

    return (
        <div>
            The Name: { name }
            <button onClick={() => setName('Another name')}></button>   
        </div>
    )
}

// another component
function Compo2() {
    const [name] = useGlobalState('name');

    return <div>{name}</div>
}

Whenever the button is clicked, all components that use the name globalState will be updated (Ex: Compo2).

  • store.init sets the starting values.
  • You can also set a default value as useGlobalState(key, defaultValue), which will be used when a global state for that key is not defined.

About

Global State for Preact

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published