Installation
npm install use-classic-state
The problem
While refactoring old code we don't want to split existing state (which can be complex ☝️) and rewrite it to multiple useState
The solution / Usage
A well tested custom hook with callback fn
as a 2nd argument for setState
, which is available for setState
only in class components!
Basic Example
const App = { const state setState = const increment = { } return <div> <div>👋</div> <div onClick=increment>statecount</div> </div> }
Example with callback
🆘🚨⚡️☝️
Important note - it is recommended that your callback
fn
is void function
In most cases you want this to do some
closeModal
ofnotification
things.
The worst case - you want to read already updated state here, but this is not recommended, instead do this with
useUffect
; ; const App = { const state setState = const increment = { ) } return <div> <div onClick=increment>👋</div> <div> statecount // 10 -> 11 after `setState` </div> <div> stateerror // false </div> </div> }
Example with Partial state (will be merged with prevState)
const App = { const state setState = const setTo100 = { } return <div> <div onClick=setTo100>👋</div> // 10 -> 100 after `setState` <div>statecount</div> // false <div>stateerror</div> </div> }