Sub topic 9
Attaching Subcomponents and Guarding Misuse
This chapter assumes you know function components, useState, and the Context API basics from the previous chapter. It covers two details of compound components: how subcomponents are attached to the parent, and how to fail clearly when they are used in the wrong place.
Attaching subcomponents as static properties
A function in JavaScript is an object, so you can add properties to it. Compound components use this to group related pieces under one name:
Tabs.Tab = Tab;
Tabs.Panel = Panel;Consumers then write <Tabs.Tab> and <Tabs.Panel>. This has three benefits:
- One import gives access to the whole family of components.
- The dot syntax shows which pieces belong together.
- Names such as
TabandPanelcannot collide with other components in the consumer's file.
The alternative is to export each piece by name (import { Tabs, Tab, Panel }). That also works and is easier for bundlers to tree-shake. The static-property style is a convention, not a requirement.
Guarding against misuse
Tab and Panel depend on state from Tabs. If someone renders <Tabs.Tab> outside <Tabs>, useContext returns the default value. The example created the context with createContext(null), so the destructuring in Tab would throw Cannot destructure property 'value' of null. That message says nothing about the real mistake.
A small custom hook turns this into a clear error:
import { createContext, useContext } from 'react';
const TabsContext = createContext(null);
function useTabs() {
const ctx = useContext(TabsContext);
if (!ctx) {
throw new Error('Tabs subcomponents must be rendered inside <Tabs>');
}
return ctx;
}Every subcomponent calls useTabs() instead of useContext(TabsContext). Using null as the default is what makes the check possible. If you gave the context a plausible default object, misuse would go unnoticed.
Common mistakes
- Forgetting to attach.
Tabs.Panelisundefinedif you never assign it. React reports "Element type is invalid", which points at the missing assignment. - Exporting the context. If consumers can import
TabsContext, they can build components that depend on its internal shape. Keep it private to the module and export only the components. - Attaching before definition.
Tabs.Tab = Tabmust come afterTabis declared when you useconstarrow functions. Function declarations are hoisted, so the example works either way.
Summary of the pattern
| Concern | Technique |
|---|---|
| Group related components | Static properties such as `Tabs.Tab` |
| Share state without props | Private context created in the same module |
| Fail clearly on misuse | A `useTabs` hook that throws when the context is `null` |
Exercise
A teammate renders <Tabs.Panel value="one"> on its own, outside any <Tabs>. With the useTabs hook shown above, what happens, and why does createContext(null) matter for this?
Show answer
useTabs throws "Tabs subcomponents must be rendered inside <Tabs>". Outside a provider, useContext returns the default value, which is null, so the if (!ctx) check detects the missing provider. A non-null default would pass the check and hide the mistake.