Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the <div {...props}> in the below code?

import React from 'react'

export default (props) => (
    <div {...props}>
        <svg focusable="false" fill="white" viewBox="0 0 24 24" aria-hidden="true">
            <path d="M12 3v10.55c-.59-.34-1.27-.55-2-.55-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z"></path>
        </svg>
    </div>
)

In the above code we are passing props inside the div element. Let's not worry about the svg tag, but how the code is working from React perspective?

like image 400
anuragp Avatar asked Nov 29 '25 13:11

anuragp


1 Answers

{...props} means we will pass all props/attributes to that element. For example, you have a few props like below

const { width, height, isOn, isRed } = props

If you want to pass all these props, the manual way can be

<div width={width} height={height} isOn={isOn} isRed={isRed}></div>

It's still good, but what if you have like 10 more props needing to pass?

The simple way can be

<div {...props}></div>

All props inside props will be passing automatically

You can check the full document here for better understanding

like image 107
Nick Vu Avatar answered Dec 01 '25 09:12

Nick Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!