Skip to content
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

fix(TabsProvider): remove uuidv4 call every render #686

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/components/TabsProvider/TabsProvider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export default {
},
};

const ExampleComponent = () => {
const ExampleComponent = (props) => {
const [selectedTab, setSelectedTab] = useState<React.Key>('tab-1');

return (
<TabsProvider selectedTab={selectedTab}>
<TabsProvider selectedTab={selectedTab} {...props}>
<TabList onTabSelection={setSelectedTab}>
<Tab key="tab-1">Tab 1</Tab>
<Tab key="tab-2">Tab 2</Tab>
Expand Down
3 changes: 2 additions & 1 deletion src/components/TabsProvider/TabsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { TabsProviderContext } from './TabsProvider.utils';
const TabsProvider: FC<Props> = (props: Props) => {
const { selectedTab, id: _id, children } = props;

const [id] = useState(_id || uuidv4());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't believe the original implementation here is calling the uuidv4 function on every render.
useState only evaluates the initial state once on first render, then keep tracks of the value internally

const [uuid] = useState(uuidv4);
const id = _id || uuid;

const getTabId = useCallback((key: React.Key) => `${id}${key}`, [id]);
const getPanelId = useCallback((key: React.Key) => `${getTabId(key)}-TabPanel`, [getTabId]);
Expand Down
Loading