Skip to content
Advertisement

My hooks aren’t opening and closing my modals

I’m new to react and redux, I’m trying to do things the newish hooks way and running into issues opening and closing a Modal using a redux state.

Basically, as soon as my page loads, the modal opens, even though the initial state in the slice is set to false and the close button in the modal footer doesn’t close it.

I’m trying to learn from the example that compiles from npx create-react-app redux-demo --template redux but I’m clearly missing something.

Thanks!

AffinityModal.js

JavaScript

}

export default AffinityModal

modalSlice.js

JavaScript

Advertisement

Answer

You don’t need toggleAffinAsync at all. Just use the regular action creator toggleAffinityModal.

affinOpen is a selector function. It is not a value. Right now your Modal is always open because you are passing this function to the isOpen prop and a function is truthy when cast to a boolean.

In order to get the boolean value from the state, you need to call affinOpen with useSelector.

JavaScript

Code Sandbox Demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement