Skip to content

Use react-freeze to optimize TabController pages #1639

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

Merged
merged 8 commits into from
Nov 22, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TabControllerScreen extends Component<{}, State> {
}

renderTabPages() {
const {asCarousel} = this.state;
const {asCarousel, fewItems} = this.state;
const Container = asCarousel ? TabController.PageCarousel : View;
const containerProps = asCarousel ? {} : {flex: true};
return (
Expand All @@ -134,7 +134,7 @@ class TabControllerScreen extends Component<{}, State> {
<Tab3/>
</TabController.TabPage>

{_.map(_.takeRight(TABS, TABS.length - 3), (title, index) => {
{!fewItems && _.map(_.takeRight(TABS, TABS.length - 3), (title, index) => {
return (
<TabController.TabPage key={title} index={index + 3}>
<View padding-s5>
Expand Down
13 changes: 12 additions & 1 deletion demo/src/screens/componentScreens/TabControllerScreen/tab3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Card, Avatar, View, Text} from 'react-native-ui-lib';

class Tab2 extends Component {
state = {
counter: 0,
loading: true
};

Expand All @@ -13,7 +14,17 @@ class Tab2 extends Component {
this.setState({loading: false});
}, 1200);

// this.slow();
/* Uncomment to test TabPage freeze functionality when the page lose focus */
// setInterval(() => {
// this.setState({counter: this.state.counter + 1});
// }, 1000);
}

componentDidUpdate() {
/* Uncomment to test TabPage freeze functionality when the page lose focus */
// if (this.state.counter % 3 === 0) {
// console.warn('freeze counter', this.state.counter);
// }
}

slow(iterations = 10) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"memoize-one": "^5.0.5",
"moment": "^2.24.0",
"prop-types": "^15.5.10",
"react-freeze": "^1.0.0",
"react-native-color": "0.0.10",
"react-native-redash": "^12.0.3",
"react-native-text-size": "4.0.0-rc.1",
Expand Down
26 changes: 21 additions & 5 deletions src/components/tabController/TabPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {PropsWithChildren, useCallback, useContext, useState, useMemo} from 'react';
import {StyleSheet} from 'react-native';
import Reanimated, {useAnimatedStyle, useAnimatedReaction, runOnJS} from 'react-native-reanimated';
import {Freeze} from 'react-freeze';
import TabBarContext from './TabBarContext';

export interface TabControllerPageProps {
Expand Down Expand Up @@ -37,8 +38,9 @@ export default function TabPage({
renderLoading,
...props
}: PropsWithChildren<TabControllerPageProps>) {
const {currentPage, targetPage, asCarousel, containerWidth} = useContext(TabBarContext);
const {currentPage, asCarousel, containerWidth} = useContext(TabBarContext);
const [shouldLoad, setLoaded] = useState(!lazy);
const [focused, setFocused] = useState(false);

const lazyLoad = useCallback(() => {
if (lazy && !shouldLoad) {
Expand All @@ -47,13 +49,26 @@ export default function TabPage({
}, [lazy, shouldLoad]);

useAnimatedReaction(() => {
return targetPage.value === index;
return currentPage.value;
},
isActive => {
(currentPage, previousPage) => {
const isActive = currentPage === index;
const wasActive = previousPage === index;
const nearActive = asCarousel && (currentPage - 1 === index || currentPage + 1 === index);
const wasNearActive =
asCarousel && previousPage !== null && (previousPage - 1 === index || previousPage + 1 === index);

if (isActive) {
runOnJS(lazyLoad)();
}
});

if (isActive || nearActive) {
runOnJS(setFocused)(true);
} else if (wasActive || wasNearActive) {
runOnJS(setFocused)(false);
}
},
[currentPage]);

const animatedPageStyle = useAnimatedStyle(() => {
const isActive = Math.round(currentPage.value) === index;
Expand All @@ -70,7 +85,8 @@ export default function TabPage({
return (
<Reanimated.View style={style} testID={testID}>
{!shouldLoad && renderLoading?.()}
{shouldLoad && props.children}
{/* {shouldLoad && props.children} */}
<Freeze freeze={!shouldLoad || !focused}>{props.children}</Freeze>
</Reanimated.View>
);
}
Expand Down