I am building a project using typescript and react, with Redux Toolkit.
Now I find that I need to write sometime the same selectors again and again.
for example const userRole = useAppSelector((state)=> state.user.role)
,
I want a method to write it once and export it, is there a way to make this
more modular?
thank you!
Question posted in Javascript
A very good W3school tutorial can be found here.
A very good W3school tutorial can be found here.
2
Answers
You literally write a selector function.
And then
You can create custom hook
And then use it like this, inside a component or inside another custom hook: