useAppDispatch 的优点 / 如何使用 useAppDispatch
Advantages of useAppDispatch / How to use useAppDispatch
鉴于RTK官方文章:
https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type
但是它是否利用 useAppDispatch
提供任何优势?
和这个有什么不同吗...
const dispatch = useDispatch()
...
dispatch(increment(42))
到这个?
const dispatch = useAppDispatch()
...
dispatch(increment(42)) // am I missing something important here?
正常的 Redux Dispatch
类型不知道您的商店可能有任何活动的中间件 - 并且中间件可以更改您的调度结果。
因此,通过正常调度 (AppDispatch
),您将得到
const myThunk = () => () => { return 5 }
const result = dispatch(myThunk())
// result is of type `() => number`
通过正确的调度,您会得到真实的结果:
const myThunk = () => () => { return 5 }
const result = dispatch(myThunk())
// result is of type `number`
鉴于RTK官方文章:
https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type
但是它是否利用 useAppDispatch
提供任何优势?
和这个有什么不同吗...
const dispatch = useDispatch()
...
dispatch(increment(42))
到这个?
const dispatch = useAppDispatch()
...
dispatch(increment(42)) // am I missing something important here?
正常的 Redux Dispatch
类型不知道您的商店可能有任何活动的中间件 - 并且中间件可以更改您的调度结果。
因此,通过正常调度 (AppDispatch
),您将得到
const myThunk = () => () => { return 5 }
const result = dispatch(myThunk())
// result is of type `() => number`
通过正确的调度,您会得到真实的结果:
const myThunk = () => () => { return 5 }
const result = dispatch(myThunk())
// result is of type `number`