Skip to content

Instantly share code, notes, and snippets.

@wuzzeb
Created September 29, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuzzeb/9e9a7f611fedf9164d594b9783893ad6 to your computer and use it in GitHub Desktop.
Save wuzzeb/9e9a7f611fedf9164d594b9783893ad6 to your computer and use it in GitHub Desktop.
Using the type-safe redux store in a component
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