Find the app root's component and put its current content inside <ModalProvider/> as so:
./App.js
importReactfrom'react'import{Text,View}from'react-native'import{ModalProvider}from'react-native-modalfy'constApp=()=> (<ModalProvider><Text>Welcome to React Native!</Text><Text>To get started, edit App.js</Text></ModalProvider>)exportdefaultApp
You can be working on a more complex application with a lot of providers already. No worries to have here: React Native Modalfy can work wherever we render it in the app root component:
You'll notice that we put <ModalProvider/> inside of <ApolloProvider/> and Redux's <Provider/> just in case modal components might need data from them.
If we run the previous examples, we'd get an error from the library. That's because <ModalProvider/> is missing a stack of modals: let's fix that!
For this step, we'll use createModalStack() and pass its output to <ModalProvider/> through the only prop the component accepts: stack. createModalStack() accepts 2 arguments: a modal configuration object (mandatory) and adefault options object (optional):
Inside modalConfig by providing a modalOptionsobject instead of just the component directly
Inside the modal component file itself, via static modalOptions (Class) or MyModalComponent.modalOptions(Hooks)
In this example, <ErrorModal/> is a regular React component we're using as our 1st modal. React Native Modalfy will register it under the key 'ErrorModal'.
Given that we're working with normal JavaScript objects here, you can change that key by simply changing the modalConfig key, ie: const modalConfig = { MyCustomErrorModalName: ErrorModal }.
We only covered the simplest way of setting up a modal stack here. Feel free to check out the createModalStack() API reference to have more in-depth explanations of createModalStack()mechanics.
We now have a fully functional modal system, ready to be used and we'll see how in the following section.