disable useSelector re-rendering #4308
Answered
by
markerikson
arashi-dev
asked this question in
Help
-
hello I have a function in my hook which gets a data from redux and do some stuff with it. const useMyHook = () => {
const myReduxState = useSelector(state => state)
const func = () => {
postData(myReduxState)
}
return { func }
} so, in this example, I don't need to re-render the component every time states update. because I only want to get it when the function gets called. this is what I've figured out to do with the help of refs: const useMyHook = () => {
const myReduxStateRef = useRef()
useSelector(state => {
myReduxStateRef.current = state
return null
})
const func = () => {
postData(myReduxStateRef.current)
}
return { func }
} now, my question is, is it a bad practice? |
Beta Was this translation helpful? Give feedback.
Answered by
markerikson
Mar 6, 2022
Replies: 1 comment
-
Very bad, and you should never do this!. If you only need that value when the function gets called, move the logic into a thunk and dispatch the thunk from a React event handler: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
arashi-dev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very bad, and you should never do this!.
If you only need that value when the function gets called, move the logic into a thunk and dispatch the thunk from a React event handler:
https://redux.js.org/usage/writing-logic-thunks