colorfy-software / react-native-modalfy

🥞 Modal citizen of React Native.
https://colorfy-software.gitbook.io/react-native-modalfy
MIT License
1.08k stars 44 forks source link

Keep params updated in openModal() #25

Closed okcompewter closed 3 years ago

okcompewter commented 4 years ago

I'm passing the props of the containing component (which includes a user object) as a param in the openModal method.

openModal('InventoryModal', props)

When the player makes an action that causes the user object to update in the parent component, the change is not updated within the modal.

I assumed this would just work like regular FC props, where if {user} prop being passed in is updated, it would also update the view within the modal. Is this not the case? If not, how can I keep the params in sync like props do typically when using functional components?

PS - I also posted the question on Stack Overflow (but couldn't tag it, as my reputation isn't the 1500 required to create a new tag): https://stackoverflow.com/questions/63625365/keep-params-updated-in-open-modal-when-using-react-native-modalfy

CharlesMangwa commented 3 years ago

Hey @okcompewter! Whenever you use openModal(), the params you provide are passed to a regular JS function, so no React reconciliation happens there.

What I would recommend would be to have a single source of truth where both your component and modal will be looking. If you're using Redux, Recoil, Context or any other state management solution for instance: your user would perform an action that would be saved in that "global" user object. Your modal would also be connected to that same user object so that it'll also update based on that user changes. Doing things this way would remove the need to pass user as a param and you could simply write openModal('InventoryModal').

You can check the last example in the ModalComponentProp section of the documentation where the modal is being updated by changes on the favouritePokemon key of the user reducer of the Redux store.

So overall you were right: given that modals are just your normal React components, you can indeed work with them as such. You just need to be careful where/how you get their props.

Let me know if this helps and looking forward to hearing more about your app and how Modalfy helped there!

okcompewter commented 3 years ago

Thanks for confirming, @CharlesMangwa ! I've opted to utilize the native Context API, and it's working great.

Modalfy has helped me solve the scenario where I need to allow custom Toast messages to appear ON TOP of any open modal. Using the native Modal component previously kept the Modal on the native UI (above the app), so there is no way to get a toast from my app to appear on top of it. Thanks again!