Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does {...props} in this React Component mean?

Just starting out with react-router.

I'm using react-router@next (version 4) when I came across this bit of code in github (at the bottom). I have weak React + ES6-fu thus require your help.

  1. Does {...props} here refer to sending props to the component?
  2. How does {...props} here impact custom="prop"?

z

<Match pattern="/foo" 
       render={(props) => ( 
         <YourRouteComponent {...props} custom="prop"/> 
       )} 
/>
like image 894
Snail Avatar asked Dec 05 '16 05:12

Snail


People also ask

What is props in React with example?

When React sees an element representing a user-defined component, it passes JSX attributes and children to this component as a single object. We call this object “props”. For example, this code renders “Hello, Sara” on the page: function Welcome(props) { return <h1>Hello, {props.

What are props used for in components?

“Props” stands for properties. It is a special keyword in React which is used for passing data from one component to another. Logically, components are just like JavaScript functions. They accept random inputs (called “props”) and return React elements which tell what should be displayed on the screen.

What does props in React mean?

Props are arguments passed into React components. Props are passed to components via HTML attributes. props stands for properties.

What is component and props in React?

React allows us to pass information to a Component using something called props (stands for properties). Props are objects which can be used inside a component. We will learn about these in detail in this article. Passing and Accessing props. We can pass props to any component as we declare attributes for any HTML tag.


1 Answers

consider the below example:

props = {
    propA: "valueA",
    propB: "valueB",
    propC: "valueC"
};

then,

<SomeComponent {...props} />

is equivalent to

<SomeComponent propA="valueA" propB="valueB" propC="valueC" />

<SomeComponent {...props} propC="someOtherValue" />

equivalent to

<SomeComponent propA="valueA" propB="valueB" propC="someOtherValue" />

Also please note that

<SomeComponent propC="someOtherValue" {...props} />

will become

<SomeComponent propA="valueA" propB="valueB" propC="valueC" />

The order is important.

Read more on JSX Spread Operator ...

like image 62
yadhu Avatar answered Oct 21 '22 05:10

yadhu