Search code examples
javascriptreactjsreduxreact-reduxredux-toolkit

How to unsubscribe to Redux store when using the useSelector in react FC


I had a single reducer big store and that store is use by multiple page and only one page is rendered at a time so if we change something on the state then other un-mounted page will be updated or Not??

if yes how to prevent that. if No then Does useSelector automatically unsubscribe the component from store updates when it is unmounted.

info : using same selector callback on every page just selecting the hole state. and using reduxjs/toolkit

found on similar question Link

But No Mention on react-redux docs or any where else on web.


Solution

  • useSelector only subscribes to the state while a component is mounted. It unsubscribes as soon as your component unmounts.