Sub topic 4
Passing numbers, booleans, objects, arrays, and functions
This chapter assumes you can write a function component, read props from its parameter, and use destructuring. Here you will learn how to pass values that are not strings.
Quotes versus curly braces
A prop written with quotes is always a string. A prop written with curly braces holds the result of any JavaScript expression.
<Avatar name="Grace" size="64" /> {/* size is the string "64" */}
<Avatar name="Grace" size={64} /> {/* size is the number 64 */}Use braces for every value that is not a string literal.
| Value type | How to pass it | Example |
|---|---|---|
| String | Quotes or braces | `label="Save"` |
| Number | Braces | `maxVisible={2}` |
| Boolean | Braces, or name only for `true` | `disabled={false}`, `disabled` |
| Object | Double braces for a literal | `style={{ color: 'red' }}` |
| Array | Braces | `items={[1, 2, 3]}` |
| Function | Braces | `onSelect={handleSelect}` |
The double braces on an object are not special syntax. The outer pair is the JSX expression slot, and the inner pair is the object literal.
One component, every type
This example passes all six kinds of value to a single component.
function ProductList({ items, maxVisible, showPrices, style, onSelect }) {
return (
<ul style={style}>
{items.slice(0, maxVisible).map((item) => (
<li key={item.id} onClick={() => onSelect(item.id)}>
{item.name}
{showPrices && ` - $${item.price}`}
</li>
))}
</ul>
);
}
export default function App() {
const items = [
{ id: 1, name: 'Keyboard', price: 49 },
{ id: 2, name: 'Mouse', price: 25 },
{ id: 3, name: 'Monitor', price: 180 },
];
return (
<ProductList
items={items}
maxVisible={2}
showPrices
style={{ listStyle: 'none', padding: 0 }}
onSelect={(id) => console.log('Selected product', id)}
/>
);
}itemsis an array of objects, and the child can call array methods such assliceandmapon it.maxVisibleis a real number, soslice(0, maxVisible)works as expected.showPriceswith no value is shorthand forshowPrices={true}.styleis an object that the child hands straight to the<ul>.onSelectis a function. The child decides when to call it, and the parent decides what it does.
Common mistakes
Booleans written as strings
<Modal open="false" /> passes the string "false". Any non-empty string is truthy, so the modal opens. Write open={false}, or leave the prop out if false is the default.
Calling a function instead of passing it
<button onClick={handleClick()}>Save</button> {/* runs during render */}
<button onClick={handleClick}>Save</button> {/* runs on click */}
<button onClick={() => handleClick(id)}>Save</button> {/* runs on click, with an argument */}The first line calls handleClick while React renders, then passes its return value as the prop. Pass the function itself, or wrap it in an arrow function when you need to give it arguments.
Numbers that arrive as strings
Values read from form inputs or URLs are strings. Convert them with Number(value) before passing them, or comparisons like size > 10 may behave in surprising ways.
Objects, arrays, and identity
An object, array, or arrow function written inline is created again on every render of the parent. It has the same contents but is a new reference each time.
<Chart options={{ animate: true }} onHover={() => {}} />This is fine for most components. It matters when a child uses memo or when the prop is a dependency of an effect, because both compare references. A later chapter covers this in detail. For now, move constant objects outside the component, or define them once above the return.
Exercise
A developer writes <Counter start="5" step={2} active="false" />. Which props have the wrong type or value, and how do you fix each one?
Show answer
start="5" is the string "5", so use start={5}. active="false" is the string "false", which is truthy, so use active={false}. step={2} is already a number and is correct.