Naveera A.
—You may have come across JSX code in React like this snippet:
<Image {...aspects} source="img_source">
And wondered what these three dots ...
are and what they do.
These three dots are called the spread syntax or spread operator. The spread syntax is a feature of ES6, and it’s also used in React.
Spread syntax allows you to deconstruct an array or object into separate variables. Even though the syntax doesn’t look particularly meaningful when you first encounter it, spread syntax is super useful.
According to MDN:
Spread syntax (
...
) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
This means is that you can use the array syntax in function calls, in array literals, and in object literals.
In React, you can use spread syntax to pass objects such as props
without the need to pass individual values.
For example, these three components are equal:
function App1() { return <Image width="50" height="100" source="img_source" />; } function App2() { const props = { width: "50", height: "100" }; return ( <Image width={props.width} height={props.height} source="img_source" /> ); } function App3() { const props = { width: "50", height: "100" }; return <Image {...props} source="img_source" />; }
This syntax is particularly useful when you want to pass a dynamic object and do not know beforehand which properties might be present in it.
Another popular use case of spread syntax is to copy objects or arrays. Spread operator can create a new object or array with all of the properties or elements of the existing object or array.
For example:
const books = [ { name: "War and Peace", read: true }, { name: "Count of Monte Christo", read: true }, ]; function addNewBook(newBook) { return [...books, { ...newBook, read: false }]; } const updatedBookList = addNewBook({ name: "Lord of the Rings" });
In React, this comes in handy when you do not want to mutate specific data like state
.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.