Difference between useSelector and useAppSelector?
Asked Answered
L

1

10

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?

Labdanum answered 14/8, 2022 at 15:11 Comment(0)
P
13

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.

Pilfer answered 14/8, 2022 at 15:27 Comment(3)
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.