Note Composition via props is preferred over inheritance. Passing components as props gives you flexible "slot" patterns without tightly coupling parent and child.
function Notification({ message, type }) {
if (!message) returnnull;
return (
<div className={`alert alert-${type}`}>
{type === "error" ? "Error: " : "Info: "}
{message}
</div>
);
}
output
Returns null when no message. Otherwise renders an alert with type prefix.
Note Returning null renders nothing. Watch out with &&: if the left side is 0, React renders "0" because it is a valid JSX child. Use Boolean(count) && <Component /> or count > 0 && <Component /> instead.
Note Keys must be stable, unique among siblings, and derived from data (like a database ID). Never use array index as key if the list order can change -- it causes subtle rendering bugs and broken state.
render listmap arraylist keyrender arraydynamic list
Fragments
syntax
<React.Fragment>...</React.Fragment>
// Short syntax
<>...</>
Note Fragments let you group elements without adding extra DOM nodes. Use the long syntax <React.Fragment key={...}> when mapping a list, since the short syntax does not support the key attribute.
fragmentgroup elementsno wrapper divmultiple elementsadjacent elements