Search code examples
reactjsredux

Can I use redux just for event driven business logic?


I have only seen redux being used hand in hand with React (or I suppose other popular frameworks) and each state change is related to a UI change. I was thinking if I could use redux totally separated from a React component to achieve event driven programming. But React gives me the useEffect hook to listen to variable changes.

So does redux provides an event listener out of the box ?
I want to keep the useSelector functionality to get the desired state variable, but then I want to replace useEffect with another event listener on this variable in order to perform some logic or dispatch another action. I don't need to render something, just to apply certain code depending on the state of the app. Can this be done only in redux environment ?


Solution

  • You can totally do that!

    If you only want to listen for state changes, you can use store.addListener. If you want to observer actions and the according before/after state, you can write your own middleware.