1.0.0 • Published 4 years ago

storu v1.0.0

Weekly downloads
3
License
ISC
Repository
-
Last release
4 years ago

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})
}