커스텀 탭 컴포넌트를 사용하는 중에 해당 탭을 클릭하면 탭이 다시 렌더링 되도록 하는 기능이 필요했는데 react 특성상 탭을 클릭한다고 해서 해당 탭을 리렌더링 하지 않았다.
function CustomTabWithPanel(props: Props): JSX.Element {
const {
tabList,
footer,
tabComponent,
isButtonType = false,
className,
selectedTab,
childrenProps = {},
reset,
...rest
} = props;
const [updateTrigger, setUpdateTrigger] = useState<boolean>(false);
const handleTabChange = useCallback((nextTab: number) => {
// 추가된 부분 ==============>
if (nextTab === state.currentTab) {
setUpdateTrigger(!updateTrigger);
}
//=========================/
dispatch(actions.changeTab(nextTab));
if (typeof reset === 'function') reset();
}, []);
return (
<Box className={className} {...rest}>
<TabContext.Provider value={{ state, dispatch }}>
<Box>
<BoTabs
tabComponent={tabComponent}
currentTab={state.currentTab}
tabList={tabList}
onChange={handleTabChange}
isButtonType={isButtonType}
/>
</Box>
{tabList.map((tab) => (
<CustomTabPanel
// key 변경 ===============>
key={`${tab.key}-${updateTrigger}`}
// =======================/
currentTab={state.currentTab}
index={Number(tab.key)}
>
<Box sx={{ pt: 1 }}>{React.cloneElement(tab.component as any, { ...childrenProps })}</Box>
</BoTabPanel>
))}
</TabContext.Provider>
{footer}
</Box>
);
}