diff --git a/packages/mui-lab/src/TabContext/TabContext.d.ts b/packages/mui-lab/src/TabContext/TabContext.d.ts index f37f7c88a3f22e..d93937ab7319fe 100644 --- a/packages/mui-lab/src/TabContext/TabContext.d.ts +++ b/packages/mui-lab/src/TabContext/TabContext.d.ts @@ -14,6 +14,10 @@ export interface TabContextProps { * The value of the currently selected `Tab`. */ value: string; + /** + * The optional id prefix, internally used to render buttons + */ + idPrefix?: string; } /** * diff --git a/packages/mui-lab/src/TabContext/TabContext.js b/packages/mui-lab/src/TabContext/TabContext.js index e2858736361b5b..e8b306b67b31e6 100644 --- a/packages/mui-lab/src/TabContext/TabContext.js +++ b/packages/mui-lab/src/TabContext/TabContext.js @@ -9,17 +9,17 @@ if (process.env.NODE_ENV !== 'production') { Context.displayName = 'TabContext'; } -function useUniquePrefix() { - const [id, setId] = React.useState(null); - React.useEffect(() => { - setId(`mui-p-${Math.round(Math.random() * 1e5)}`); - }, []); - return id; +function useUniquePrefix(idPrefix) { + const ref = React.useRef() + if (!ref.current || (typeof idPrefix === 'string' && ref.current !== idPrefix)) { + ref.current = idPrefix || `mui-p-${Math.round(Math.random() * 1e5)}` + } + return ref.current; } export default function TabContext(props) { - const { children, value } = props; - const idPrefix = useUniquePrefix(); + const { children, value, idPrefix: idPrefixProp } = props; + const idPrefix = useUniquePrefix(idPrefixProp); const context = React.useMemo(() => { return { idPrefix, value }; @@ -37,6 +37,10 @@ TabContext.propTypes /* remove-proptypes */ = { * The content of the component. */ children: PropTypes.node, + /** + * The optional id prefix, internally used to render buttons + */ + idPrefix: PropTypes.string, /** * The value of the currently selected `Tab`. */