storu

1.0.0 • Public • Published

storu

usage

import {StoruProvider} from 'storu'
import * as actions from './actions'
 
function App(){
    return <StoruProvider actions={actions}>
        <Auth />
    </StoruProvider>
}
 
function Auth(){
    const storu = useStoru()
    const {user,actions,setState} = storu
 
    const isSignedIn = user&&user.name
    return <div>
        <span>{user.name}</span>
        {!isSignedIn ?
            <button onClick={actions.login('fognet','*****')}> 
                sign in!
            </button> :
            <button onClick={()=>setState({user:null})}>
                click to logout
            </button>
        }
    </div>
}

create your actions

storu is injected as the last argument in each "action", with two methods:

  • storu.setState: set state into the context
  • storu.setStore: set state that is also persisted to localStorage
export function login(username, password, storu) {
    const user = await axios.post('/loginroute', {username,password})
    storu.setState({user})
}

Readme

Keywords

none

Package Sidebar

Install

npm i storu

Weekly Downloads

7

Version

1.0.0

License

ISC

Unpacked Size

3 kB

Total Files

3

Last publish

Collaborators

  • fognet