Hung P.T.
Hung P.T.

Reputation: 141

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?

Upvotes: 2

Views: 2870

Answers (1)

phry
phry

Reputation: 44186

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`

Upvotes: 3

Related Questions