I came across useAppSelector while using redux with RTK query. So what will be the difference between useSelector and useAppSelector gonna be or both works in the same way?
Difference between useSelector and useAppSelector?
Asked Answered
On a runtime level, they are 100% equal. The difference is that one is untyped so the state
variable is unknown
and needs to be manually typed by you on every single call of useSelector
- and you can pass in everything wrong there without causing an error.
useAppSelector
on the other hand gets declared with your RootState
in one place and after that it's typesafe, ready to be used everywhere in your application.
That is why useAppSelector and useAppDispatch is recommended to be used with TypeScript. –
Labdanum
so useApp* is for typescript & useSelector/useDispatch is for plainJS? –
Saintebeuve
Yes, TypeScript needs some extra information, so you have to create those custom-typed hooks. –
Pilfer
© 2022 - 2024 — McMap. All rights reserved.