UNPKG

12.7 kBMarkdownView Raw
1<img src="https://user-images.githubusercontent.com/16062886/117443651-c13d9500-af38-11eb-888d-b6a0b580760c.png" width="100%" alt="React Native Screens by Software Mansion" >
2
3This project aims to expose native navigation container components to React Native. It is not designed to be used as a standalone library but rather as a dependency of a [full-featured navigation library](https://github.com/react-navigation/react-navigation).
4
5## Fabric
6
7To learn about how to use `react-native-screens` with Fabric architecture, head over to [Fabric README](README-Fabric.md). Instructions on how to run Fabric Example within this repo can be found in the [FabricExample README](FabricExample/README.md).
8
9## Supported platforms
10
11- [x] iOS
12- [x] Android
13- [x] tvOS
14- [x] visionOS
15- [x] Windows
16- [x] Web
17
18## Installation
19
20### iOS
21
22Installation on iOS is completely handled with auto-linking, if you have ensured pods are installed after adding this module, no other actions are necessary.
23
24### Android
25
26On Android the View state is not persisted consistently across Activity restarts, which can lead to crashes in those cases. It is recommended to override the native Android method called on Activity restarts in your main Activity, to avoid these crashes.
27
28For most people using an app built from the react-native template, that means editing `MainActivity.java`, likely located in `android/app/src/main/java/<your package name>/MainActivity.java`
29
30You should add this code, which specifically discards any Activity state persisted during the Activity restart process, to avoid inconsistencies that lead to crashes.
31Please note that the override code should not be placed inside `MainActivityDelegate`, but rather directly in `MainActivity`.
32
33<details open>
34<summary>Java</summary>
35
36```java
37import android.os.Bundle;
38
39public class MainActivity extends ReactActivity {
40
41 //...code
42
43 //react-native-screens override
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(null);
47 }
48
49 public static class MainActivityDelegate extends ReactActivityDelegate {
50 //...code
51 }
52}
53```
54</details>
55
56<details>
57<summary>Kotlin</summary>
58
59```kotlin
60import android.os.Bundle;
61
62class MainActivity: ReactActivity() {
63
64 //...code
65
66 //react-native-screens override
67 override fun onCreate(savedInstanceState: Bundle?) {
68 super.onCreate(null);
69 }
70}
71```
72</details>
73
74For people that must handle cases like this, there is [a more detailed discussion of the difficulties in a series of related comments](https://github.com/software-mansion/react-native-screens/issues/17#issuecomment-424704633).
75
76<details>
77<summary>Need to use a custom Kotlin version?</summary>
78<br>
79
80Since `v3.6.0` `react-native-screens` has been rewritten with Kotlin. Kotlin version used in this library defaults to `1.4.10`.
81
82If you need to use a different Kotlin version, set `kotlinVersion` ext property in your project's `android/build.gradle` and the library will use this version accordingly:
83
84```
85buildscript {
86 ext {
87 ...
88 kotlinVersion = "1.4.10"
89 }
90}
91```
92
93**Disclaimer**: `react-native-screens` requires Kotlin `1.3.50` or higher.
94
95</details>
96
97### Windows
98
99Installation on Windows should be completely handled with auto-linking when using React Native Windows 0.63+. For earlier versions, you must [manually link](https://microsoft.github.io/react-native-windows/docs/native-modules-using) the native module.
100
101## How can I take advantage of that?
102
103Screens are already integrated with the React Native's most popular navigation library [react-navigation](https://github.com/react-navigation/react-navigation) and [Expo](https://expo.io).
104
105## Supported react-native version
106
107| library version | react-native version |
108| ------- | -------------------- |
109| 3.32.0+ | 0.71.0+ |
110| 3.30.0+ | 0.68.0+ |
111| 3.14.0+ | 0.64.0+ |
112| 3.0.0+ | 0.62.0+ |
113| 2.0.0+ | 0.60.0+ |
114
115### Support for Fabric
116
117[Fabric](https://reactnative.dev/architecture/fabric-renderer) is React Native's new rendering system.
118
119Here's a table with summary of supported `react-native` versions when Fabric is turned on.
120
121| library version | react-native version |
122| ------- | -------------------- |
123| 3.32.0+ | 0.74.0+ |
124| 3.28.0+ | 0.73.0+ |
125| 3.21.0+ | 0.72.0+ |
126| 3.19.0+ | 0.71.0+ |
127| 3.18.0+ | 0.70.0+ |
128| 3.14.0+ | 0.69.0+ |
129
130## Usage with [react-navigation](https://github.com/react-navigation/react-navigation)
131
132Screens support is built into [react-navigation](https://github.com/react-navigation/react-navigation) starting from version [2.14.0](https://github.com/react-navigation/react-navigation/releases/tag/2.14.0) for all the different navigator types (stack, tab, drawer, etc).
133
134To configure react-navigation to use screens instead of plain RN Views for rendering screen views, simply add this library as a dependency to your project:
135
136```bash
137# bare React Native project
138yarn add react-native-screens
139
140# if you use Expo managed workflow
141npx expo install react-native-screens
142```
143
144Just make sure that the version of [react-navigation](https://github.com/react-navigation/react-navigation) you are using is 2.14.0 or higher.
145
146You are all set 🎉 – when screens are enabled in your application code react-navigation will automatically use them instead of relying on plain React Native Views.
147
148### Experimental support for `react-freeze`
149
150> You have to use React Native 0.68 or higher, react-navigation 5.x or 6.x and react-native-screens >= v3.9.0
151
152Since `v3.9.0`, `react-native-screens` comes with experimental support for [`react-freeze`](https://github.com/software-mansion-labs/react-freeze). It uses the React `Suspense` mechanism to prevent parts of the React component tree from rendering, while keeping its state untouched.
153
154To benefit from this feature, enable it in your entry file (e.g. `App.js`) with this snippet:
155
156```js
157import { enableFreeze } from 'react-native-screens';
158
159enableFreeze(true);
160```
161
162Want to know more? Check out [react-freeze README](https://github.com/software-mansion-labs/react-freeze#readme)
163
164Found a bug? File an issue [here](https://github.com/software-mansion/react-native-screens/issues) or directly in [react-freeze repository](https://github.com/software-mansion-labs/react-freeze/issues).
165
166### Disabling `react-native-screens`
167
168If, for whatever reason, you'd like to disable native screens support and use plain React Native Views add the following code in your entry file (e.g. `App.js`):
169
170```js
171import { enableScreens } from 'react-native-screens';
172
173enableScreens(false);
174```
175
176You can also disable the usage of native screens per navigator with [`detachInactiveScreens`](https://reactnavigation.org/docs/stack-navigator#detachinactivescreens).
177
178### Using `createNativeStackNavigator` with React Navigation
179
180To take advantage of the native stack navigator primitive for React Navigation that leverages `UINavigationController` on iOS and `Fragment` on Android, please refer:
181
182- for React Navigation >= v6 to the [Native Stack Navigator part of React Navigation documentation](https://reactnavigation.org/docs/native-stack-navigator)
183- for React Navigation v5 to the [README in react-native-screens/native-stack](https://github.com/software-mansion/react-native-screens/tree/main/native-stack)
184
185## `FullWindowOverlay`
186
187Native `iOS` component for rendering views straight under the `Window`. Based on `RCTPerfMonitor`. You should treat it as a wrapper, providing full-screen, transparent view which receives no props and should ideally render one child `View`, being the root of its view hierarchy. For the example usage, see https://github.com/software-mansion/react-native-screens/blob/main/TestsExample/src/Test1096.tsx
188
189## Interop with [react-native-navigation](https://github.com/wix/react-native-navigation)
190
191React-native-navigation library already uses native containers for rendering navigation scenes so wrapping these scenes with `<ScreenContainer>` or `<Screen>` component does not provide any benefits. Yet if you would like to build a component that uses screens primitives under the hood (for example a view pager component) it is safe to use `<ScreenContainer>` and `<Screen>` components for that as these work out of the box when rendered on react-native-navigation scenes.
192
193## Interop with other libraries
194
195This library should work out of the box with all existing react-native libraries. If you experience problems with interoperability please [report an issue](https://github.com/software-mansion/react-native-screens/issues).
196
197## Guide for navigation library authors
198
199If you are building a navigation library you may want to use `react-native-screens` to have control over which parts of the React component tree are attached to the native view hierarchy.
200To do that, `react-native-screens` provides you with the components documented [here](https://github.com/software-mansion/react-native-screens/tree/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md).
201
202## Common problems
203
204### Problems with header on iOS
205
206- [Focused search bar causes new screens to have incorrect header](https://github.com/software-mansion/react-native-screens/issues/996)
207- [Scrollable content gets cut off by the header with a search bar](https://github.com/software-mansion/react-native-screens/issues/1120)
208- [RefreshControl does not work properly with NativeStackNavigator and largeTitle](https://github.com/software-mansion/react-native-screens/issues/395)
209
210#### Solution
211
212Use `ScrollView` with prop `contentInsetAdjustmentBehavior=“automatic”` as a main container of the screen and set `headerTranslucent: true` in screen options.
213
214### Other problems
215
216| Problem | Solution |
217| -------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
218| [SVG component becomes transparent when goBack](https://github.com/software-mansion/react-native-screens/issues/773) | [related PRs](https://github.com/software-mansion/react-native-screens/issues/773#issuecomment-783469792) |
219| [Memory leak while moving from one screen to another in the same stack](https://github.com/software-mansion/react-native-screens/issues/843) | [explanation](https://github.com/software-mansion/react-native-screens/issues/843#issuecomment-832034119) |
220| [LargeHeader stays small after pop/goBack/swipe gesture on iOS 14+](https://github.com/software-mansion/react-native-screens/issues/649) | [potential fix](https://github.com/software-mansion/react-native-screens/issues/649#issuecomment-712199895) |
221| [`onScroll` and `onMomentumScrollEnd` of previous screen triggered in bottom tabs](https://github.com/software-mansion/react-native-screens/issues/1183) | [explanation](https://github.com/software-mansion/react-native-screens/issues/1183#issuecomment-949313111) |
222
223## Contributing
224
225There are many ways to contribute to this project. See [CONTRIBUTING](https://github.com/software-mansion/react-native-screens/tree/main/guides/CONTRIBUTING.md) guide for more information. Thank you for your interest in contributing!
226
227## License
228
229React native screens library is licensed under [The MIT License](LICENSE).
230
231## Credits
232
233This project has been build and is maintained thanks to the support from [Shopify](https://shopify.com), [Expo.io](https://expo.io), and [Software Mansion](https://swmansion.com).
234
235[![shopify](https://avatars1.githubusercontent.com/u/8085?v=3&s=100 'Shopify.com')](https://shopify.com)
236[![expo](https://avatars2.githubusercontent.com/u/12504344?v=3&s=100 'Expo.io')](https://expo.io)
237[![swm](https://logo.swmansion.com/logo?color=white&variant=desktop&width=150&tag=react-native-reanimated-github 'Software Mansion')](https://swmansion.com)
238
239## React Native Screens is created by Software Mansion
240
241Since 2012 [Software Mansion](https://swmansion.com) is a software agency with experience in building web and mobile apps. We are Core React Native Contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product – [Hire us](https://swmansion.com/contact/projects?utm_source=screens&utm_medium=readme).