'Advantages of useAppDispatch / How to use useAppDispatch
Given the official article of RTK:
https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type
But does it provide any advantage utilizing useAppDispatch?
Is there any different from this...
const dispatch = useDispatch()
...
dispatch(increment(42))
to this?
const dispatch = useAppDispatch()
...
dispatch(increment(42)) // am I missing something important here?
Solution 1:[1]
The normal Redux Dispatch type has no knowledge of any middleware your store might have active - and middleware can change your dispatch result.
So with normal dispatch (AppDispatch), you would get
const myThunk = () => () => { return 5 }
const result = dispatch(myThunk())
// result is of type `() => number`
with a correct dispatch you would get the real result thought:
const myThunk = () => () => { return 5 }
const result = dispatch(myThunk())
// result is of type `number`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
