33. Defining a Type for Props with (children)
Раздел 3 Using TypeScript with React - Esentials Component Props & The Special "key" Prop All React components (built-in components and also your custom components) do accept a special key prop which is used by React to track specific component instances. For example, the key prop should always be set when outputting a list of components. This key prop can be set on custom components even if you didn't specify it in your props type! For example, the following component code will work: type UserProps = { name: string; }; function User({ name }: UserProps) { return User: {name}; } function App() { const users = [{ name: 'John' }, { name: 'Mary' }, { name: 'Luke' }]; return ( ﹤﹥ {users.map((user, index) =﹥ ( ))} ); }
Раздел 3 Using TypeScript with React - Esentials Component Props & The Special "key" Prop All React components (built-in components and also your custom components) do accept a special key prop which is used by React to track specific component instances. For example, the key prop should always be set when outputting a list of components. This key prop can be set on custom components even if you didn't specify it in your props type! For example, the following component code will work: type UserProps = { name: string; }; function User({ name }: UserProps) { return User: {name}; } function App() { const users = [{ name: 'John' }, { name: 'Mary' }, { name: 'Luke' }]; return ( ﹤﹥ {users.map((user, index) =﹥ ( ))} ); }
