Type checking with TypeScript/Flow
React Native Modalfy has type definitions for both TypeScript & Flow projects. The library itself uses Flow types for now. So if you do too: you get type checking out of the box, you're good to go. For TypeScript projects, all the types you need are exposed by the library: let's see how to use them together.
Type checking ModalProvider
/ createModalStack
ModalProvider
/ createModalStack
We don't need to do anything here: the library automatically covers these!
Type checking the modal
prop
modal
propAs we've seen in the Opening & closing guide, there are 2 ways to access the modal
prop: either from a modal component (provided by the library itself) or anywhere else in our code (via useModal
/withModal
).
From a modal component
Given that this component is an item of the modal stack, we'll need to type the modal
prop with ModalStackItemProp
as so:
import React from 'react'
import { ModalStackItemProp } from 'react-native-modalfy'
interface Props {
modal: ModalStackItemProp
}
class MyModal extends React.Component<Props> {
// ...
}
From anywhere else in the code
This time, ModalProp
is the interface we'll need:
import React from 'react'
import { ModalProp, withModal } from 'react-native-modalfy'
interface Props {
modal: ModalStackItemProp
}
const MyComponent = ({ modal }: Props): JSX.Element => (
// ...
)
export default withModal(MyComponent)
Type checking static modalOptions
static modalOptions
Besides the default modal options passed to createModalStack()
, another way to define a modal's options is to used static modalOptions
if you're inside a class. Its type is also provided as so:
import React from 'react'
import { ModalStackItemProp, ModalStackOptions } from 'react-native-modalfy'
interface Props {
modal: ModalStackItemProp
}
class MyModalComponent extends React.Component<Props> {
static modalOptions: ModalStackOptions = {
containerStyle: {
borderWidth: 5,
borderColor: 'rebeccapurple'
},
}
// ...
}
export default MyModalComponent
Last updated
Was this helpful?