Spread props in React

Hi, in this post we are going to see how Spread props is applying to a component (React Fundamentals).

If we have this structure:

const myClass = 'container'
const children = 'Hello World'
const props = {myClass, children}

console.log('props--->', props)

The result will be an object (properties and values):

Then, if we want to apply that prop to this div element:

const children = 'Hello World'

const element = <div>{children}</div>

Instead we will have this code:

    const myClass = 'container'
    const children = 'Hello World'
    const props = {myClass, children}

    const element = <div />

Then we can Spread the props like this:

const myClass = 'container'
const children = 'Hello World'
const props = {myClass, children}

const element = <div {...props} />

Result View:

React code in the browser:

By Cristina Rojas.