React Native Web Modal
modal-react-native-web is a JavaScript library that provides a web-compatible implementation of the core `Modal` component from React Native. It aims to replicate the API, behavior, and design of the native React Native Modal for projects utilizing `react-native-web`. The current stable version is `0.2.0`, last updated in 2018. Due to its age and reliance on outdated peer dependencies (React 16.x.x, React Native Web 0.9.x), its release cadence has ceased, and it is largely considered unmaintained for modern React/React Native Web environments. While it offers a solution for older `react-native-web` setups, `react-native-web` itself now includes a native `Modal` component, rendering this package largely obsolete for new projects.
Common errors
-
npm WARN modal-react-native-web@0.2.0 requires a peer of react@16.x.x but none is installed. You must install peer dependencies yourself.
cause The installed `react` version is incompatible with the peer dependency specified by `modal-react-native-web`.fixThis package is largely unmaintained and expects older React versions. The most robust fix is to remove `modal-react-native-web` and use the `Modal` component directly from a recent `react-native-web` version. If you must use this package, downgrade your `react`, `react-dom`, `react-art`, and `react-native-web` packages to their respective `16.x.x` and `0.9.x` versions. -
TypeError: (0, _reactNativeWeb.Modal) is not a function or is not defined
cause This error often occurs when `react-native-web` is installed, but `modal-react-native-web` is not, or the import path is incorrect, or the version of `react-native-web` does not yet expose a native `Modal` component.fixFor modern `react-native-web` (v0.13.0+), simply import `Modal` from `'react-native'`. If using an older `react-native-web` and still needing this package, ensure `modal-react-native-web` is installed and imported as a default export: `import Modal from 'modal-react-native-web';`.
Warnings
- breaking This package has strict peer dependencies on `react@16.x.x`, `react-dom@16.x.x`, `react-art@16.x.x`, and `react-native-web@0.9.x`. Using it with newer versions of React (v17+) or React Native Web (v0.10+ up to current v0.21+) will likely lead to dependency conflicts or runtime errors.
- deprecated The functionality provided by `modal-react-native-web` is now natively supported by `react-native-web` itself. Using this package is no longer necessary or recommended for modern `react-native-web` applications.
- gotcha Not all props available in the original React Native Modal are supported by this `modal-react-native-web` implementation.
- gotcha In versions prior to 0.2.0, the backdrop might not show if the `isVisible` prop was set to `true` when the component mounted, and there were potential issues with unsafe unmounting. These were addressed in v0.2.0.
Install
-
npm install modal-react-native-web -
yarn add modal-react-native-web -
pnpm add modal-react-native-web
Imports
- Modal
import { Modal } from 'modal-react-native-web';import Modal from 'modal-react-native-web';
- Modal
const { Modal } = require('modal-react-native-web');const Modal = require('modal-react-native-web'); - Example
import { Example } from './Example';import Example from './Example';
Quickstart
import React, { Component } from 'react';
import { Text, TouchableHighlight, View } from 'react-native';
import Modal from 'modal-react-native-web';
export default class Example extends Component {
state = {
modalVisible: false,
};
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
render() {
return (
<View style={{marginTop: 22}}>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onDismiss={() => {
// In a real app, you might navigate away or perform other cleanup
console.log('Modal has been closed.');
}}>
<View style={{marginTop: 22}}>
<View>
<Text>Hello World!</Text>
<TouchableHighlight
onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<TouchableHighlight
onPress={() => {
this.setModalVisible(true);
}}>
<Text>Show Modal</Text>
</TouchableHighlight>
</View>
);
}
}