15

A piece of react-native code:

enderScene(route, navigator) {
   let Component = route.component;
   return (
      <Component {...route.params} navigator={navigator}></Component>
   );
}

this code returns a Component Object ,

But I don't understand this code ---> {...route.params}

Question:

  1. What is meant by '...' ?
  2. Can you tell me what is meant by " {...route.params}" ?
Lian van der Vyver
  • 2,096
  • 1
  • 17
  • 26
user5465320
  • 719
  • 2
  • 7
  • 15
  • 1
    http://stackoverflow.com/questions/31048953/what-does-the-three-dots-in-react-do – vinayr Oct 19 '16 at 07:46
  • Does this answer your question? [What do these three dots in React do?](https://stackoverflow.com/questions/31048953/what-do-these-three-dots-in-react-do) – Henke Mar 16 '21 at 07:56

4 Answers4

31

The '...' is called Spread syntax.

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected.

Example :

var car = ["door", "motor", "wheels"];
var truck = [...car, "something", "biggerthancar"];

// truck  = ["door", "motor", "wheels", "something", "biggerthancar"]

If you want to know more about spread operator :

https://dmitripavlutin.com/how-three-dots-changed-javascript/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41
Nicolas Menettrier
  • 1,647
  • 1
  • 13
  • 28
13

To expand on the above, in the context of the original post the spread operator is essentially passing through all of the parameters in route.params

For example if route.params was the object

{key: 'my-route', title: 'My Route Title'}

Then

<Component {...route.params} navigator={navigator} />

Could be re-written as

<Component key={route.params.key} title={route.params.title}  navigator={navigator} />

The other "side" of this is the destructuring assignment (example using stateless react components)

const Component = (props) => {
    // this is simply referencing the property by the object key
    let myKey = props.key
    // this is using destructuring and results in the variables key, title and navigator which are from props.key, props.title and props.navigator
    let { key, title, navigator } = props

    return <Text>{title}</Text>
}

You can also do this in the function declaration like so which achieves the same thing

const Component = ({key, title, navigator}) => {
    // now you have variables key, title and navigator 
    return <Text>{title}</Text>
}

See Destructuring

Sam Parmenter
  • 1,831
  • 2
  • 11
  • 17
3

Ok, I was confused about that for a long period of time. So, I'll try my best to explain it to you:

Suppose, you've a react class like bellow:

import React, {Component} from 'react';
import { Link } from 'react-router-dom';



class SingleService extends Component{

    render(){
        return(
            <div class="col-md-4">
                <span class="fa-stack fa-4x">
                <i class="fas fa-circle fa-stack-2x text-primary"></i>
                <i class={`fas ${this.props.icon} fa-stack-1x fa-inverse`}></i>
                </span>
        <h4 class="service-heading">{this.props.title}</h4>
        <p class="text-muted">{this.props.description}</p>
            </div>
        );
    }

}
export default SingleService;

Here, you can see that there are so many {this.props.variable}. Those are used to create dynamic values when we import this above class into another class, like bellow:

import React, {Component} from 'react';
import { Link } from 'react-router-dom';

import SingleService from './SingleService';
// declaring a constant array to hold all of our services props.
// The following array is made up of the objects.

const services = [
    {
        title:'E-commerce',
        description:'Description text on E-commerce',
        icon: 'fa-shopping-cart'
    }
];
class Services extends Component{
    render(){
        return(
            <div>
  <section class="page-section" id="services">
    <div class="container">
      <div class="row">
        <div class="col-lg-12 text-center">
          <h2 class="section-heading text-uppercase">Services</h2>
          <h3 class="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
        </div>
      </div>
      <div class="row text-center">
          {/* it's looping through an object, that's why we've used key value pair. */}
          { /* 
            to write js inside JSX,  we use curly braces 
            here we're using array.map() function.
          */}
            {services.map((service, index) => {
              // returning our component with props.
              // return (<SingleService title={service.title} description={service.description} icon={service.icon} />);
              // or, we can write the following
              return (<SingleService {...service}/>);
            })}
      </div>
    </div>
  </section>
            </div>
        );
    }
}
export default Services;

Now, here, I've used the famous

return (<SingleService {...SingleService}/>);

But one very important thing, I could avoid using it simply by writing the following line:

return (<SingleService title={service.title} description={service.description} icon={service.icon} />);

So, you can see in the send return statement, I've specified all of the props variables individually and assigned values to those, whereas in the first return statement, I've passed in all pf the props together from the SingleService object at once, that will pass all od the key-value pairs.

Subhasish Nath
  • 124
  • 1
  • 14
1

To add to the above given answers, the ... or the spread operator is not something special to react native. It is a feature in es6. ES6 stands for ecma script and is the standard followed for javascript. This means that you could create a .js file outside of react/react-native and run it in a node env and the spread operator would still work.

Yash Kalwani
  • 443
  • 4
  • 11