Skip to content

Commit

Permalink
CP-7211: Fix TabViewAva bug to show indexes as titles after fast refr…
Browse files Browse the repository at this point in the history
…esh (#908)
  • Loading branch information
onghwan authored Sep 12, 2023
1 parent 7c3db62 commit fce0b50
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions app/components/TabViewAva.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useCallback, useEffect, useMemo, useState } from 'react'
import React, { FC, useCallback, useMemo } from 'react'
import {
NavigationState,
SceneRendererProps,
Expand Down Expand Up @@ -47,25 +47,21 @@ const TabViewAva: TabViewAvaFC = ({
lazy = true,
children
}) => {
const [currentIndex, setCurrentIndex] = useState(currentTabIndex)
const theme = useApplicationContext().theme

const childrenArray = useMemo(
() => React.Children.toArray(children),
[children]
)

useEffect(() => {
setCurrentIndex(currentTabIndex)
}, [currentTabIndex])

// https://github.com/satya164/react-native-tab-view#tabview-props
const routes = useMemo(
() =>
childrenArray.map((child, index) => {
const isValidChild =
React.isValidElement(child) && child.type === TabViewAva.Item
const title = isValidChild ? child.props.title : index.toString()
const title =
React.isValidElement(child) &&
(child.type as FC).displayName === TabViewAvaItemDisplayName
? child.props.title
: index.toString()

return {
key: title,
Expand All @@ -77,8 +73,8 @@ const TabViewAva: TabViewAvaFC = ({
)

const navState = useMemo(() => {
return { index: currentIndex, routes }
}, [currentIndex, routes])
return { index: currentTabIndex, routes }
}, [currentTabIndex, routes])

const scenes = useCallback(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -90,7 +86,6 @@ const TabViewAva: TabViewAvaFC = ({

const handleIndexChange = useCallback(
(index: number) => {
setCurrentIndex(index)
onTabIndexChange?.(index)
},
[onTabIndexChange]
Expand Down Expand Up @@ -181,4 +176,7 @@ const TabViewAva: TabViewAvaFC = ({

TabViewAva.Item = ({ children }) => <>{children}</>

const TabViewAvaItemDisplayName = 'TabViewAva.Item'
TabViewAva.Item.displayName = TabViewAvaItemDisplayName

export default TabViewAva

0 comments on commit fce0b50

Please sign in to comment.