Performance: Props and component composition
This affects how fast components render and update by controlling data flow and component nesting.
const Parent = () => {
const data = { a: 1 };
return <Child a={data.a} />;
};
const Child = ({ a }) => {
return <GrandChild a={a} />;
};
const GrandChild = ({ a }) => {
return <div>{a}</div>;
};const Parent = () => {
const data = { a: 1, b: 2, c: 3, d: 4 };
return <Child {...data} />;
};
const Child = (props) => {
return <GrandChild {...props} />;
};
const GrandChild = (props) => {
return <div>{props.a}</div>;
};| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing all props through many layers | High (many props passed) | Multiple reflows due to re-renders | High paint cost | [X] Bad |
| Passing only needed props | Low (minimal props) | Single reflow per update | Low paint cost | [OK] Good |
| Deep nesting without memo | Moderate DOM nodes | Repeated reflows on interaction | Moderate paint cost | [X] Bad |
| Deep nesting with memo | Moderate DOM nodes | Minimal reflows | Low paint cost | [OK] Good |
props in Astro components?title with value "Hello" to a child component <MyComponent /> in Astro?<MyComponent title="Hello" /> uses correct syntax: title="Hello". <MyComponent {title: "Hello"} /> uses object syntax which is invalid. <MyComponent title={Hello} /> misses quotes around Hello. <MyComponent title=>Hello</MyComponent> uses children syntax which is not how props are passed.---
const { message } = Astro.props;
---
<div>{message}</div><Child message="Hi!" />?message from Astro.props, which receives the prop from the parent.message inside a <div>, so it renders <div>Hi!</div>.<ChildComponent message=Hello />
message=Hello without quotes, causing a syntax error.Item components, each receiving a name prop from an array ['Apple', 'Banana', 'Cherry']. Which Astro code correctly composes these components?map is used to create multiple components.---
const fruits = ['Apple', 'Banana', 'Cherry'];
---
{fruits.map(fruit => )} correctly maps over fruits and passes each fruit as a name prop. --- const fruits = ['Apple', 'Banana', 'Cherry']; --- <Item name="fruits" />passes the whole array as a string prop, not individual items.
---
const fruits = ['Apple', 'Banana', 'Cherry'];
---
{fruits.forEach(fruit => )} uses forEach which returns undefined, so no output. ---
const fruits = ['Apple', 'Banana', 'Cherry'];
---
<Item>{fruits}</Item> passes the whole array as children, not as props.Pass the test and this pattern moves unknown