Skip to content

Commit

Permalink
fix: use route keys instead of index for lazy load
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Oct 7, 2020
1 parent 16e7ac1 commit c49dab3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 7 additions & 7 deletions packages/bottom-tabs/src/views/BottomTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Props = BottomTabNavigationConfig & {
};

type State = {
loaded: number[];
loaded: string[];
};

function SceneContent({
Expand Down Expand Up @@ -54,18 +54,18 @@ export default class BottomTabView extends React.Component<Props, State> {
};

static getDerivedStateFromProps(nextProps: Props, prevState: State) {
const { index } = nextProps.state;
const focusedRouteKey = nextProps.state.routes[nextProps.state.index].key;

return {
// Set the current tab to be loaded if it was not loaded before
loaded: prevState.loaded.includes(index)
loaded: prevState.loaded.includes(focusedRouteKey)
? prevState.loaded
: [...prevState.loaded, index],
: [...prevState.loaded, focusedRouteKey],
};
}

state = {
loaded: [this.props.state.index],
state: State = {
loaded: [this.props.state.routes[this.props.state.index].key],
};

private renderTabBar = () => {
Expand Down Expand Up @@ -103,7 +103,7 @@ export default class BottomTabView extends React.Component<Props, State> {
return null;
}

if (lazy && !loaded.includes(index) && !isFocused) {
if (lazy && !loaded.includes(route.key) && !isFocused) {
// Don't render a screen if we've never navigated to it
return null;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/drawer/src/views/DrawerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function DrawerView({
minSwipeDistance,
sceneContainerStyle,
}: Props) {
const [loaded, setLoaded] = React.useState([state.index]);
const [loaded, setLoaded] = React.useState([state.routes[state.index].key]);
const dimensions = useWindowDimensions();

const { colors } = useTheme();
Expand Down Expand Up @@ -129,8 +129,10 @@ export default function DrawerView({
return () => subscription?.remove();
}, [handleDrawerClose, isDrawerOpen, navigation, state.key]);

if (!loaded.includes(state.index)) {
setLoaded([...loaded, state.index]);
const focusedRouteKey = state.routes[state.index].key;

if (!loaded.includes(focusedRouteKey)) {
setLoaded([...loaded, focusedRouteKey]);
}

const renderNavigationView = ({ progress }: any) => {
Expand Down Expand Up @@ -159,7 +161,7 @@ export default function DrawerView({
return null;
}

if (lazy && !loaded.includes(index) && !isFocused) {
if (lazy && !loaded.includes(route.key) && !isFocused) {
// Don't render a screen if we've never navigated to it
return null;
}
Expand Down

0 comments on commit c49dab3

Please sign in to comment.