├── .circleci └── config.yml ├── .editorconfig ├── .eslintrc ├── .gitattributes ├── .gitignore ├── .prettierrc.js ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── babel.config.js ├── commitlint.config.js ├── package.json ├── react-native-heavy-screen.podspec ├── src ├── __tests__ │ └── index.test.tsx ├── heavy-screen.tsx ├── index.tsx ├── optimize-heavy-screen.tsx └── use-after-interactions.ts ├── tsconfig.json └── yarn.lock /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | defaults: &defaults 4 | docker: 5 | - image: circleci/node:10 6 | working_directory: ~/project 7 | 8 | jobs: 9 | install-dependencies: 10 | <<: *defaults 11 | steps: 12 | - checkout 13 | - attach_workspace: 14 | at: ~/project 15 | - restore_cache: 16 | keys: 17 | - dependencies-{{ checksum "package.json" }} 18 | - dependencies- 19 | - restore_cache: 20 | keys: 21 | - dependencies-example-{{ checksum "example/package.json" }} 22 | - dependencies-example- 23 | - run: | 24 | yarn install --cwd example --frozen-lockfile 25 | yarn install --frozen-lockfile 26 | - save_cache: 27 | key: dependencies-{{ checksum "package.json" }} 28 | paths: node_modules 29 | - save_cache: 30 | key: dependencies-example-{{ checksum "example/package.json" }} 31 | paths: example/node_modules 32 | - persist_to_workspace: 33 | root: . 34 | paths: . 35 | lint: 36 | <<: *defaults 37 | steps: 38 | - attach_workspace: 39 | at: ~/project 40 | - run: | 41 | yarn lint 42 | typescript: 43 | <<: *defaults 44 | steps: 45 | - attach_workspace: 46 | at: ~/project 47 | - run: yarn typescript 48 | unit-tests: 49 | <<: *defaults 50 | steps: 51 | - attach_workspace: 52 | at: ~/project 53 | - run: yarn test --coverage 54 | - store_artifacts: 55 | path: coverage 56 | destination: coverage 57 | build-package: 58 | <<: *defaults 59 | steps: 60 | - attach_workspace: 61 | at: ~/project 62 | - run: yarn prepare 63 | 64 | 65 | workflows: 66 | version: 2 67 | build-and-test: 68 | jobs: 69 | - install-dependencies 70 | - lint: 71 | requires: 72 | - install-dependencies 73 | - typescript: 74 | requires: 75 | - install-dependencies 76 | - unit-tests: 77 | requires: 78 | - install-dependencies 79 | - build-package: 80 | requires: 81 | - install-dependencies 82 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | # root = true 6 | 7 | # [*] 8 | 9 | # indent_style = tab 10 | # indent_size = 4 11 | 12 | # end_of_line = lf 13 | # charset = utf-8 14 | # trim_trailing_whitespace = true 15 | # insert_final_newline = true 16 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["nando"], 3 | "rules": { 4 | "@typescript-eslint/indent": "off" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.pbxproj -text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # XDE 6 | .expo/ 7 | 8 | # VSCode 9 | .vscode/ 10 | jsconfig.json 11 | 12 | # Xcode 13 | # 14 | build/ 15 | *.pbxuser 16 | !default.pbxuser 17 | *.mode1v3 18 | !default.mode1v3 19 | *.mode2v3 20 | !default.mode2v3 21 | *.perspectivev3 22 | !default.perspectivev3 23 | xcuserdata 24 | *.xccheckout 25 | *.moved-aside 26 | DerivedData 27 | *.hmap 28 | *.ipa 29 | *.xcuserstate 30 | project.xcworkspace 31 | 32 | # Android/IJ 33 | # 34 | .idea 35 | .gradle 36 | local.properties 37 | android.iml 38 | 39 | # Cocoapods 40 | # 41 | example/ios/Pods 42 | 43 | # node.js 44 | # 45 | node_modules/ 46 | npm-debug.log 47 | yarn-debug.log 48 | yarn-error.log 49 | 50 | # BUCK 51 | buck-out/ 52 | \.buckd/ 53 | android/app/libs 54 | android/keystores/debug.keystore 55 | 56 | # generated by bob 57 | lib/ 58 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | ...require('eslint-config-nando/prettier'), 3 | useTabs: false, 4 | tabWidth: 2, 5 | } 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We want this community to be friendly and respectful to each other. Please follow it in all your interactions with the project. 4 | 5 | ## Development workflow 6 | 7 | To get started with the project, run `yarn bootstrap` in the root directory to install the required dependencies for each package: 8 | 9 | ```sh 10 | yarn bootstrap 11 | ``` 12 | 13 | While developing, you can run the [example app](/example/) to test your changes. 14 | 15 | To start the packager: 16 | 17 | ```sh 18 | yarn example start 19 | ``` 20 | 21 | To run the example app on Android: 22 | 23 | ```sh 24 | yarn example android 25 | ``` 26 | 27 | To run the example app on iOS: 28 | 29 | ```sh 30 | yarn example android 31 | ``` 32 | 33 | Make sure your code passes TypeScript and ESLint. Run the following to verify: 34 | 35 | ```sh 36 | yarn typescript 37 | yarn lint 38 | ``` 39 | 40 | To fix formatting errors, run the following: 41 | 42 | ```sh 43 | yarn lint --fix 44 | ``` 45 | 46 | Remember to add tests for your change if possible. Run the unit tests by: 47 | 48 | ```sh 49 | yarn test 50 | ``` 51 | 52 | ### Commit message convention 53 | 54 | We follow the [conventional commits specification](https://www.conventionalcommits.org/en) for our commit messages: 55 | 56 | - `fix`: bug fixes, e.g. fix crash due to deprecated method. 57 | - `feat`: new features, e.g. add new method to the module. 58 | - `refactor`: code refactor, e.g. migrate from class components to hooks. 59 | - `docs`: changes into documentation, e.g. add usage example for the module.. 60 | - `test`: adding or updating tests, eg add integration tests using detox. 61 | - `chore`: tooling changes, e.g. change CI config. 62 | 63 | Our pre-commit hooks verify that your commit message matches this format when committing. 64 | 65 | ### Linting and tests 66 | 67 | [ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/) 68 | 69 | We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint](https://eslint.org/) with [Prettier](https://prettier.io/) for linting and formatting the code, and [Jest](https://jestjs.io/) for testing. 70 | 71 | Our pre-commit hooks verify that the linter and tests pass when committing. 72 | 73 | ### Scripts 74 | 75 | The `package.json` file contains various scripts for common tasks: 76 | 77 | - `yarn bootstrap`: setup project by installing all dependencies and pods. 78 | - `yarn typescript`: type-check files with TypeScript. 79 | - `yarn lint`: lint files with ESLint. 80 | - `yarn test`: run unit tests with Jest. 81 | - `yarn example start`: start the Metro server for the example app. 82 | - `yarn example android`: run the example app on Android. 83 | - `yarn example ios`: run the example app on iOS. 84 | 85 | ### Sending a pull request 86 | 87 | > **Working on your first pull request?** You can learn how from this _free_ series: [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github). 88 | 89 | When you're sending a pull request: 90 | 91 | - Prefer small pull requests focused on one change. 92 | - Verify that linters and tests are passing. 93 | - Review the documentation to make sure it looks good. 94 | - Follow the pull request template when opening a pull request. 95 | - For pull requests that change the API or implementation, discuss with maintainers first by opening an issue. 96 | 97 | ## Code of Conduct 98 | 99 | ### Our Pledge 100 | 101 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 102 | 103 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 104 | 105 | ### Our Standards 106 | 107 | Examples of behavior that contributes to a positive environment for our community include: 108 | 109 | - Demonstrating empathy and kindness toward other people 110 | - Being respectful of differing opinions, viewpoints, and experiences 111 | - Giving and gracefully accepting constructive feedback 112 | - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 113 | - Focusing on what is best not just for us as individuals, but for the overall community 114 | 115 | Examples of unacceptable behavior include: 116 | 117 | - The use of sexualized language or imagery, and sexual attention or 118 | advances of any kind 119 | - Trolling, insulting or derogatory comments, and personal or political attacks 120 | - Public or private harassment 121 | - Publishing others' private information, such as a physical or email 122 | address, without their explicit permission 123 | - Other conduct which could reasonably be considered inappropriate in a 124 | professional setting 125 | 126 | ### Enforcement Responsibilities 127 | 128 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 129 | 130 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 131 | 132 | ### Scope 133 | 134 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 135 | 136 | ### Enforcement 137 | 138 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [INSERT CONTACT METHOD]. All complaints will be reviewed and investigated promptly and fairly. 139 | 140 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 141 | 142 | ### Enforcement Guidelines 143 | 144 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 145 | 146 | #### 1. Correction 147 | 148 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 149 | 150 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 151 | 152 | #### 2. Warning 153 | 154 | **Community Impact**: A violation through a single incident or series of actions. 155 | 156 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 157 | 158 | #### 3. Temporary Ban 159 | 160 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 161 | 162 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 163 | 164 | #### 4. Permanent Ban 165 | 166 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 167 | 168 | **Consequence**: A permanent ban from any sort of public interaction within the community. 169 | 170 | ### Attribution 171 | 172 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, 173 | available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 174 | 175 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 176 | 177 | [homepage]: https://www.contributor-covenant.org 178 | 179 | For answers to common questions about this code of conduct, see the FAQ at 180 | https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. 181 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Fernando Rojo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ⚡️ Speed up heavy React Native screens 2 | 3 | Optimize heavy screens in **React Native** to prevent lags with React Navigation's stack. 4 | 5 | This isn't actually specific to React Navigation, but I find myself using it there often. 6 | 7 | Especially useful for screens that set up listeners, make network requests, etc. 8 | 9 | ## Usage 10 | 11 | 🥳 New component-based API! Use this if you only want to optimize certain content on your screen. 12 | 13 | ```jsx 14 | import React from 'react' 15 | import { OptimizedHeavyScreen } from 'react-navigation-heavy-screen' 16 | 17 | const Screen = () => ( 18 | <> 19 | 20 | 21 | 22 | 23 | 24 | ) 25 | ``` 26 | 27 | You can also use the normal export usage. Use this if you want to optimize your whole screen. 28 | 29 | ```js 30 | import { optimizeHeavyScreen } from 'react-navigation-heavy-screen' 31 | 32 | const Screen = () => ... 33 | 34 | export default optimizeHeavyScreen(Screen, OptionalPlaceHolderScreen) 35 | ``` 36 | 37 | Or you can require your heavy screen inline: 38 | 39 | ```js 40 | import { optimizeHeavyScreen } from 'react-navigation-heavy-screen' 41 | 42 | export default optimizeHeavyScreen( 43 | require('path/to/HeavyScreen'), 44 | OptionalPlaceHolderScreen 45 | ) 46 | ``` 47 | 48 | _Thanks to [@Sebastien Lorber](https://twitter.com/sebastienlorber/status/1250113509880401933) for this recommendation ^_ 49 | 50 | ## Installation 51 | 52 | ```sh 53 | yarn add react-navigation-heavy-screen 54 | ``` 55 | 56 | Install peer dependencies: 57 | 58 | ```sh 59 | expo install react-native-reanimated 60 | ``` 61 | 62 | ## What does it do? 63 | 64 | Delay rendering a component until interactions are complete, using `InteractionManager`. Then it fades in your screen. 65 | 66 | --- 67 | 68 | ## `` 69 | 70 | ### Props 71 | 72 | - `placeholder` (optional) Non-heavy React component that renders in the meantime. 73 | - Extends `Animated.View` props [docs](https://software-mansion.github.io/react-native-reanimated). So you can pass any props you need to customize the animation. eg: `{ entering: { FadeIn } }` 74 | 75 | ```js 76 | import React from 'react' 77 | import { OptimizedHeavyScreen } from 'react-navigation-heavy-screen' 78 | 79 | const Screen () => ( 80 | 81 | 82 | 83 | ) 84 | 85 | export default Screen 86 | ``` 87 | 88 | ## `optimizeHeavyScreen(Screen, Placeholder, options)` 89 | 90 | ```js 91 | import { optimizeHeavyScreen } from 'react-navigation-heavy-screen' 92 | 93 | export default optimizeHeavyScreen(Screen, OptionalPlaceHolderScreen, { 94 | // default values 95 | disableHoistStatics: false, 96 | }) 97 | ``` 98 | 99 | ### Arguments 100 | 101 | - `Screen` **required** Any React component whose render should be delayed until interactions are complete. 102 | - `Placeholder` (optional) Non-heavy React component that renders in the meantime. 103 | - `options` (optional) Dictionary with the following options: 104 | - `disableHoistStatics`: (optional) If `true`, the `Screen`'s statics (like `navigationOptions`, etc.) will not be passed on. Default: `false`. 105 | - Extends `Animated.View` props [docs](https://software-mansion.github.io/react-native-reanimated). So you can pass any props you need to customize the animation. eg: `{ entering: { FadeIn } }` 106 | 107 | ## License 108 | 109 | MIT 110 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: ['module:metro-react-native-babel-preset'], 3 | }; 4 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ['@commitlint/config-conventional'], 3 | }; 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-navigation-heavy-screen", 3 | "version": "1.2.1", 4 | "description": "Optimize heavy screens to prevent lags with React Navigation.", 5 | "main": "lib/commonjs/index.js", 6 | "module": "lib/module/index.js", 7 | "types": "lib/typescript/index.d.ts", 8 | "react-native": "src/index.tsx", 9 | "files": [ 10 | "lib", 11 | "src" 12 | ], 13 | "scripts": { 14 | "test": "jest", 15 | "typescript": "tsc --noEmit", 16 | "lint": "eslint --ext .js,.ts,.tsx .", 17 | "prepare": "bob build", 18 | "release": "release-it", 19 | "example": "yarn --cwd example", 20 | "pods": "cd example/ios && node -e \"process.exit(require('os').platform() === 'darwin')\" || pod install", 21 | "bootstrap": "yarn example && yarn && yarn pods" 22 | }, 23 | "keywords": [ 24 | "react-native", 25 | "ios", 26 | "android" 27 | ], 28 | "repository": "https://github.com/nandorojo/react-navigation-heavy-screen", 29 | "author": "Fernando Rojo (https://github.com/nandorojo)", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/nandorojo/react-native-heavy-screen/issues" 33 | }, 34 | "homepage": "https://github.com/nandorojo/react-native-heavy-screen#readme", 35 | "devDependencies": { 36 | "@commitlint/config-conventional": "^8.3.4", 37 | "@react-native-community/bob": "^0.10.0", 38 | "@react-native-community/eslint-config": "^0.0.7", 39 | "@release-it/conventional-changelog": "^1.1.0", 40 | "@types/jest": "^25.1.2", 41 | "@types/react": "^16.9.34", 42 | "@types/react-native": "^0.62.2", 43 | "commitlint": "^8.3.4", 44 | "eslint-config-nando": "^1.0.8", 45 | "husky": "^4.0.1", 46 | "jest": "^25.1.0", 47 | "react": "^16.13.1", 48 | "react-native": "~0.61.5", 49 | "react-native-reanimated": "^1.8.0", 50 | "release-it": "^12.6.3", 51 | "typescript": "^3.7.5" 52 | }, 53 | "peerDependencies": { 54 | "react": "*", 55 | "react-native": "*" 56 | }, 57 | "jest": { 58 | "preset": "react-native", 59 | "modulePathIgnorePatterns": [ 60 | "/example/node_modules", 61 | "/lib/" 62 | ] 63 | }, 64 | "eslintIgnore": [ 65 | "node_modules/", 66 | "lib/" 67 | ], 68 | "release-it": { 69 | "git": { 70 | "commitMessage": "chore: release %s", 71 | "tagName": "v%s" 72 | }, 73 | "npm": { 74 | "publish": true 75 | }, 76 | "github": { 77 | "release": true 78 | }, 79 | "plugins": { 80 | "@release-it/conventional-changelog": { 81 | "preset": "angular" 82 | } 83 | } 84 | }, 85 | "@react-native-community/bob": { 86 | "source": "src", 87 | "output": "lib", 88 | "targets": [ 89 | "commonjs", 90 | "module", 91 | "typescript" 92 | ] 93 | }, 94 | "dependencies": { 95 | "hoist-non-react-statics": "^3.3.2", 96 | "react-native-animatable": "^1.3.3" 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /react-native-heavy-screen.podspec: -------------------------------------------------------------------------------- 1 | require "json" 2 | 3 | package = JSON.parse(File.read(File.join(__dir__, "package.json"))) 4 | 5 | Pod::Spec.new do |s| 6 | s.name = "react-native-heavy-screen" 7 | s.version = package["version"] 8 | s.summary = package["description"] 9 | s.homepage = package["homepage"] 10 | s.license = package["license"] 11 | s.authors = package["author"] 12 | 13 | s.platforms = { :ios => "9.0" } 14 | s.source = { :git => "https://github.com/nandorojo/react-native-heavy-screen.git", :tag => "#{s.version}" } 15 | 16 | s.source_files = "ios/**/*.{h,m}" 17 | 18 | s.dependency "React" 19 | end 20 | -------------------------------------------------------------------------------- /src/__tests__/index.test.tsx: -------------------------------------------------------------------------------- 1 | it.todo('write a test'); 2 | -------------------------------------------------------------------------------- /src/heavy-screen.tsx: -------------------------------------------------------------------------------- 1 | import React, { ComponentPropsWithoutRef, ComponentType } from 'react' 2 | import Animated, { FadeIn, FadeOut } from 'react-native-reanimated' 3 | import { useAfterInteractions } from './use-after-interactions' 4 | 5 | interface Props extends ComponentPropsWithoutRef { 6 | children?: React.ReactNode, 7 | placeHolder?: ComponentType 8 | } 9 | 10 | const OptimizedHeavyScreen = ({ 11 | children, 12 | placeHolder: Placeholder, 13 | ...rest 14 | }: Props) => { 15 | const { transitionRef, areInteractionsComplete } = useAfterInteractions() 16 | return ( 17 | 23 | {areInteractionsComplete ? ( 24 | children 25 | ) : !!Placeholder ? ( 26 | 27 | ) : null} 28 | 29 | ) 30 | } 31 | 32 | export { OptimizedHeavyScreen } 33 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | export * from './optimize-heavy-screen' 2 | export * from './use-after-interactions' 3 | export * from './heavy-screen' 4 | -------------------------------------------------------------------------------- /src/optimize-heavy-screen.tsx: -------------------------------------------------------------------------------- 1 | import React, { ComponentType, ComponentPropsWithoutRef } from 'react' 2 | import Animated from 'react-native-reanimated' 3 | // @ts-ignore 4 | import hoistNonReactStatics from 'hoist-non-react-statics' 5 | import { useAfterInteractions } from './use-after-interactions' 6 | import { StyleSheet } from 'react-native' 7 | 8 | interface optimizeHeavyScreenOptions extends ComponentPropsWithoutRef { 9 | disableHoistStatics?: boolean 10 | } 11 | 12 | export function optimizeHeavyScreen( 13 | Component: ComponentType, 14 | Placeholder: ComponentType | null = null, 15 | options: optimizeHeavyScreenOptions = {} 16 | ) { 17 | const { 18 | disableHoistStatics = false, 19 | ...rest 20 | } = options 21 | const OptimizedHeavyScreen = (props: Props) => { 22 | const { 23 | transitionRef, 24 | areInteractionsComplete, 25 | } = useAfterInteractions() 26 | return ( 27 | 32 | {areInteractionsComplete ? ( 33 | 34 | ) : !!Placeholder ? ( 35 | 36 | ) : null} 37 | 38 | ) 39 | } 40 | if (!disableHoistStatics) { 41 | // forward navigationOptions, and other statics 42 | hoistNonReactStatics(OptimizedHeavyScreen, Component) 43 | } 44 | return OptimizedHeavyScreen 45 | } 46 | 47 | const styles = StyleSheet.create({ container: { flex: 1 } }) 48 | -------------------------------------------------------------------------------- /src/use-after-interactions.ts: -------------------------------------------------------------------------------- 1 | import { useState, useEffect, useRef } from 'react' 2 | import { InteractionManager } from 'react-native' 3 | import { TransitioningView } from 'react-native-reanimated' 4 | 5 | export const useAfterInteractions = () => { 6 | const [areInteractionsComplete, setInteractionsComplete] = useState(false) 7 | 8 | const subscriptionRef = useRef | null>(null) 11 | 12 | const transitionRef = useRef(null) 13 | 14 | useEffect(() => { 15 | subscriptionRef.current = InteractionManager.runAfterInteractions( 16 | () => { 17 | transitionRef.current?.animateNextTransition() 18 | setInteractionsComplete(true) 19 | subscriptionRef.current = null 20 | } 21 | ) 22 | return () => { 23 | subscriptionRef.current?.cancel() 24 | } 25 | }, []) 26 | 27 | return { 28 | areInteractionsComplete, 29 | transitionRef, 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "paths": { 5 | "react-native-heavy-screen": ["./src/index"] 6 | }, 7 | "allowUnreachableCode": false, 8 | "allowUnusedLabels": false, 9 | "esModuleInterop": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "jsx": "react", 12 | "lib": ["esnext"], 13 | "module": "esnext", 14 | "moduleResolution": "node", 15 | "noFallthroughCasesInSwitch": true, 16 | "noImplicitReturns": true, 17 | "noImplicitUseStrict": false, 18 | "noStrictGenericChecks": false, 19 | "noUnusedLocals": true, 20 | "noUnusedParameters": true, 21 | "resolveJsonModule": true, 22 | "skipLibCheck": true, 23 | "strict": true, 24 | "target": "esnext" 25 | } 26 | } 27 | --------------------------------------------------------------------------------