beekai-oss / little-state-machine

📠 React custom hook for persist state management
https://lrz5wloklm.csb.app/
MIT License
1.47k stars 53 forks source link

include getState function to retrieve latest store data #136

Closed bluebill1049 closed 1 year ago

bluebill1049 commented 2 years ago

Context

The new method will retrieve the latest store value when an action is performed, this is great for usage such as re-render is not required and yet you want to retrieve the newest store value.

Example

const update  = (state: GlobalState, payload: string) => {
  return {
    ...state,
    value: payload
  }
}

const { getState, actions } = useStateMachine({
  update,
});

<button onClick={() => { actions.update('test', { skipRender: true }) }}>Update<button≥ 
<button onClick={() => { getState() }}>Get State<button≥