Last active
May 7, 2020 01:07
-
-
Save UberMouse/d37c467935275ff6ecabb72072113e48 to your computer and use it in GitHub Desktop.
This file contains 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
type TContext = { | |
state: { | |
someComputedReduxState: string | |
}, | |
actions: { | |
doTheThing: () => void | |
} | |
} | |
const Context = React.createContext<TContext | null>(null); | |
function Provider(props) { | |
const someComputedState = useSelector(someSelector); | |
const doTheThing = useAction(someAction); | |
return <Context.Provider value={{state: {someComputedState}, actions: {doTheThing}}}>{props.children}</Context.Provider>; | |
} | |
const useReduxState = () => useContext(Context); | |
function Foo() { | |
const {state, actions} = useReduxState(); | |
return <p onClick={actions.doTheThing}>{state.someComputedReduxState}</p> | |
} | |
function FooWithProvider(props) { | |
return <Provider><Foo {...props} /></Provider>; | |
} | |
export { FooWithProvider as Foo, Foo as FooWithoutProvider } | |
// Then in storybook/testing | |
<Context.Provider values={{state: {someComputedReduxState: "whatever I want"}, actions: {doTheThing: action("doTheThing")}}> | |
<FooWithoutProvider /> | |
</Context.Provider> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment