Created
September 29, 2019 18:07
-
-
Save wuzzeb/9e9a7f611fedf9164d594b9783893ad6 to your computer and use it in GitHub Desktop.
Using the type-safe redux store in a component
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
import * as React from "react"; | |
import { SampleActionType, useSelector, useDispatch } from "../store"; | |
function IncrBtn(props: { value: number }) { | |
const incr = useDispatch(SampleActionType.IncrementNum); | |
//incr has type (payload: {by: number}) => void | |
return ( | |
<p> | |
<button onClick={() => incr({ by: props.value })}>Incr by {props.value}</button> | |
</p> | |
); | |
} | |
export function App() { | |
const curNum = useSelector(s => s.sample.num); | |
const decr = useDispatch(SampleActionType.DecrementNum); | |
//decr has type () => void | |
return ( | |
<div> | |
<p>Current {curNum}</p> | |
<IncrBtn value={2} /> | |
<IncrBtn value={4} /> | |
<button onClick={decr}>Decr</button> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment