Socket
Socket
Sign inDemoInstall

react-navigation-focus-render

Package Overview
Dependencies
499
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-navigation-focus-render

Prevents re-renders of components that aren't on the focused screen


Version published
Weekly downloads
586
increased by1.56%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Feature Flag, Remote Config and A/B Testing platform, Flagsmith

react-navigation-focus-render

Screens within a stack or in tabs will still render when any global state(Redux/Context etc) is updated. This component lets you avoid these renders when the screens are inactive.

Installation

npm i react-navigation-focus-render --save

Usage

import FocusRender from 'react-navigation-focus-render'

const ExpensiveComponent = () => {
   const {count} = useCount(); // something that hooks into changing state
   
   return (
     <FocusRender>
       ... Components
     </FocusRender>
   ) 
}

Advanced usage

You may wish to add a wrapper component that displays differently whilst the inactive component hydrates its state when the screen becomes active.

You can specify a Wrapper component that takes isFocused as a property.

import FocusRender from 'react-navigation-focus-render'

const Wrapper = ({isFocused, children}) => (
  <View style={{opacity: isFocused ? 1 : 0.5}}>{children}</View>
);

const ExpensiveComponent = () => {
   const {count} = useCount(); // something that hooks into changing state
   
   return (
     <FocusRender Wrapper={Wrapper}>
       ... Components
     </FocusRender>
   ) 
}

Example

You can see the example project to see this working.

HomeScreen:

  • Contains an expensive component that renders 5000 text elements and is connected to redux state "count"
  • You can toggle between rendering the component via FocusRender or just by itself

TabScreen

  • Contains a screen with a button that updates state "count"

Given this simple example, the difference in performance when updating state can be measured using https://github.com/Flagsmith/react-native-performance-monitor.

img.png

Of course, this is quite an extreme example but given an active stack of many tabs / screens this could easily add up if you have complex components.

Keywords

FAQs

Last updated on 20 Feb 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc