Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it mandatory to pass props to react functional component?

I have a functional react component. It does not use any props, it just return elements. Have I pass props to it anyway? Are there any agreements about that? Will be code below valid React code?

const HelloComponent = () => (
    <div>Hi!</div>
);
like image 519
Tatiana Avatar asked Sep 04 '19 14:09

Tatiana


2 Answers

No you don't. props are explicitly passed to each component. If you don't use any property from it, just don't declare it. Exactly like in your example. Consider the following

const App = () => <Child />

const Child = props => {
    console.log(props) // { }
    return <div>hey</div>
}

In this case props is just an empty Object and there is no need to declare the argument. If you don't need to read from it, don't even mention it

const Child = () => <div>hey</div>
like image 98
Dupocas Avatar answered Oct 22 '22 22:10

Dupocas


It is completly valid, there is no need for props. Its the same as with any other function, if it doesnt have arguments, dont give it any. :-)

like image 20
Lukáš Gibo Vaic Avatar answered Oct 22 '22 21:10

Lukáš Gibo Vaic