Skip to content

[RN][Docs] Add View Recycling guide #4575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/the-new-architecture/advanced-topics-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ This guide will cover the following topics:
- [Direct Manipulation](/docs/the-new-architecture/direct-manipulation-new-architecture)
- [Measuring the Layout](/docs/the-new-architecture/layout-measurements)
- [Invoking native functions on your native component](/docs/next/the-new-architecture/fabric-component-native-commands)
- [View Recycling](/docs/next/the-new-architecture/view-recycling)
23 changes: 23 additions & 0 deletions docs/the-new-architecture/view-recycling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# View Recycling

View recycling is a performance optimization technique used in mobile app development, particularly in list views or scrollable content. It involves reusing views that have gone off-screen instead of creating new ones. This reduces the overhead of view creation and destruction, leading to smoother scrolling and better memory management.

React Native pushes the technique of view recycling to the next level by applying it to every Native Component and by allowing developers to customize the recycling behavior of their components.

For user applications, view recycling is crucial because it enhances performance by minimizing the number of view objects that need to be created and garbage collected. This results in faster rendering times and a more responsive user interface, especially in applications with large lists or complex UI components.

## Implementing View Recycling in React Native's New Architecture

View Recycling is enabled by default in the New Architecture. However, when you are writing your custom Native Component, it is important that you keep the concept of view recycling in mind and you provide the proper implementation of the `prepareForRecycle` method so that React Native can properly reuse your component without the risk of showing stale data.

### Android


Check failure on line 15 in docs/the-new-architecture/view-recycling.md

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎⏎`

### iOS

On iOS, every Native Component conforms to the [`RCTComponentViewProtocol`](https://github.com/facebook/react-native/packages/react-native/React/Fabric/Mounting/RCTComponentViewProtocol.h) protocol which defines the [`prepareForRecycle` method](https://github.com/facebook/react-native/packages/react-native/React/Fabric/Mounting/RCTComponentViewProtocol.h#L112).

If your component contains a state that needs to reset, or is composed by native Apple components such as WKWebView and AVPlayer that needs some cleanup code when the view is removed from the hierarchy, the `prepareforRecycle` method is where you should write the cleanup code.

Examples of components that implements `prepareForRecycle` in React Native core are [RCTImageComponentView](https://github.com/facebook/react-native/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm#L113-L118), and [RCTTextInputComponentView](https://github.com/facebook/react-native/packages/react-native/React/Fabric/Mounting/ComponentViews/ImageTextInput/RCTTextInputComponentView.mm#L338-L349)
Loading