Very long re-rendering times in 0.71.3
Problem
For the latest update on this situation, please refer to this comment. ---- Description I have a complex calendar component in my app. I previously ran it on 0.66.5, where it was really fast, but after upgrading to 0.71.3, performance dropped drastically, to the point that it is unusable. The problem is most obvious on ios, but also appears on Android. I used to react devtools in flipper to measure the re-render times. On ios in 0.66.5, the component re-rendered in about 7ms, on 0.71.3 it took nearly 400ms. On android the 0.66.5 took about 10ms, 0.71.3 took about 30ms. The app uses dayjs and Flashlist, but performance was the same when switching to regular Flatlist. React Native Version 0.71.3 Output of `npx react-native info` System: OS: macOS 13.0.1 CPU: (8) arm64 Apple M1 Memory: 139.16 MB / 16.00 GB Shell: 3.3.1 - /opt/homebrew/Cellar/fish/3.3.1/bin/fish Binaries: Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.0/bin/yarn npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm Watchman: 2022.01.17.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.2 - /opt/homebrew/lib/ruby/gems/3.0.0/bin/pod SDKs: iOS SDK: Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1 Android SDK: API Levels: 29, 30, 33 Build Tools: 29.0.2, 30.0.2, 30.0.3, 33.0.0 System Images: android-30 | Google APIs Intel x86 Atom, android-S | Google APIs AR
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Optimize Calendar Component Rendering in React Native 0.71.3
The performance degradation in re-render times from version 0.66.5 to 0.71.3 is likely due to changes in React Native's reconciliation algorithm and the way components are updated. The complex calendar component may be causing excessive re-renders due to state or prop changes that trigger unnecessary updates, especially with the introduction of new features or optimizations in the newer version.
Awaiting Verification
Be the first to verify this fix
- 1
Implement React.memo for Calendar Component
Wrap the calendar component with React.memo to prevent unnecessary re-renders when props haven't changed.
javascriptconst MemoizedCalendar = React.memo(CalendarComponent); - 2
Use useCallback for Event Handlers
Ensure that event handlers passed to the calendar component are wrapped in useCallback to avoid creating new function instances on every render.
javascriptconst handleEvent = useCallback((event) => { /* event handling logic */ }, [dependencies]); - 3
Optimize State Management
Review state management within the calendar component. Use local state where possible and consider using useReducer for complex state logic to minimize re-renders.
javascriptconst [state, dispatch] = useReducer(reducer, initialState); - 4
Profile Component Performance
Use React DevTools to profile the component and identify which props or states are causing the most re-renders. Optimize those specific areas.
javascriptimport { Profiler } from 'react'; <Profiler id="Calendar" onRender={callback}><MemoizedCalendar /></Profiler> - 5
Check for Unnecessary State Updates
Audit the component for any state updates that may be causing re-renders. Ensure that setState is only called when necessary.
javascriptif (newState !== currentState) { setState(newState); }
Validation
After implementing the above steps, use React DevTools to measure the re-render times of the calendar component again. The expected re-render time should decrease significantly, ideally back to or below the original times observed in version 0.66.5.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep