You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can find a similar concept in the [wrapping components](/material-ui/guides/composition/#wrapping-components) guide.
75
-
76
74
If using a class component as a child, you'll also need to ensure that the ref is forwarded to the underlying DOM element. (A ref to the class component itself will not work.)
For example, by default a `List` component will render a `<ul>` element.
38
30
This can be changed by passing a [React component](https://react.dev/reference/react/Component) to the `component` prop.
39
-
The following example will render the `List` component with a `<nav>` element as root element instead:
31
+
The following example renders the `List` component with a `<menu>` element as root element instead:
40
32
41
33
```jsx
42
-
<List component="nav">
43
-
<ListItem button>
44
-
<ListItemText primary="Trash"/>
34
+
<List component="menu">
35
+
<ListItem>
36
+
<ListItemButton>
37
+
<ListItemText primary="Trash"/>
38
+
</ListItemButton>
45
39
</ListItem>
46
-
<ListItem button>
47
-
<ListItemText primary="Spam"/>
40
+
<ListItem>
41
+
<ListItemButton>
42
+
<ListItemText primary="Spam"/>
43
+
</ListItemButton>
48
44
</ListItem>
49
45
</List>
50
46
```
51
47
52
48
This pattern is very powerful and allows for great flexibility, as well as a way to interoperate with other libraries, such as your favorite routing or forms library.
53
-
But it also **comes with a small caveat!**
54
-
55
-
### Inlining & caveat
56
-
57
-
Using an inline function as an argument for the `component` prop may result in **unexpected unmounting**, since a new component is passed every time React renders.
58
-
For instance, if you want to create a custom `ListItem` that acts as a link, you could do the following:
However, since we are using an inline function to change the rendered component, React will remount the link every time `ListItemLink` is rendered. Not only will React update the DOM unnecessarily but the state will be lost, for example the ripple effect of the `ListItem` will also not work correctly.
81
-
:::
50
+
### Passing other React components
82
51
83
-
The solution is simple: **avoid inline functions and pass a static component to the `component` prop** instead.
84
-
Let's change the `ListItemLink` component so `CustomLink` always reference the same component:
52
+
You can pass any other React component to `component` prop. For example, you can pass `Link` component from `react-router-dom`:
You can take advantage of the prop forwarding to simplify the code.
116
-
In this example, we don't create any intermediary component:
117
-
118
-
```jsx
119
-
import { Link } from'react-router-dom';
120
-
121
-
<ListItem button component={Link} to="/">
122
-
```
123
-
124
-
:::warning
125
-
However, this strategy suffers from a limitation: prop name collisions.
126
-
The component receiving the `component` prop (for example ListItem) might intercept the prop (for example to) that is destined to the leaf element (for example Link).
127
-
:::
128
-
129
67
### With TypeScript
130
68
131
69
To be able to use the `component` prop, the type of the props should be used with type arguments. Otherwise, the `component` prop will not be present.
@@ -148,9 +86,9 @@ The other props of the `Typography` component will also be present in props of t
148
86
149
87
You can find a code example with the Button and react-router-dom in [these demos](/material-ui/integrations/routing/#component-prop).
150
88
151
-
####Generic
89
+
### Generic
152
90
153
-
It's also possible to have a generic `CustomComponent`which will accept any React component, and HTML elements.
91
+
It's also possible to have a generic custom component which accepts any React component, including [built-in components](https://react.dev/reference/react-dom/components/common).
154
92
155
93
```ts
156
94
function GenericCustomComponent<CextendsReact.ElementType>(
0 commit comments