Last active
October 18, 2022 15:52
-
-
Save shannonmoeller/b41e741384ca2e251fbc9397bd743054 to your computer and use it in GitHub Desktop.
Look, Ma! No dispatcher!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { createContext, useContext, useMemo, useState } from 'react'; | |
export const NumberContext = createContext(); | |
export function NumberContextProvider(props) { | |
const [state, setState] = useState({ | |
number: 0, | |
}); | |
// Using useState instead of useMemo so React doesn't garbage collect it. | |
// Using useState instead of useRef so we only create the actions once. | |
const [actions] = useState(() => ({ | |
increment(amount = 1) { | |
setState((prev) => ({ | |
...prev, | |
number: prev.number + amount, | |
}); | |
}, | |
decrement(amount = 1) { | |
setState((prev) => ({ | |
...prev, | |
number: prev.number - amount, | |
}); | |
}, | |
})); | |
// Prevent React from triggering needless rerenders. | |
const value = useMemo( | |
() => [state, actions], | |
[state, actions] | |
); | |
return <NumberContext.Provider {...props} value={value} />; | |
} | |
export function useNumberContext() { | |
return useContext(NumberContext); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { useNumberContext } from './context.js'; | |
export function Counter() { | |
const [state, actions] = useNumberContext(); | |
return ( | |
<div> | |
<button onClick={() => actions.decrement()}>-1</button> | |
{` ${state.number} `} | |
<button onClick={() => actions.increment(2)}>+2</button> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment