Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass props without value to component

How does one pass a prop without value to a react component?

<SomeComponent disableHeight> {/* here */}     {({width}) => (         <AnotherComponent             autoHeight {/* and here */}             width={width}             height={300}             {...otherProps}         />     )} </SomeComponent> 

Note - there is no default prop values specified for those props.

I can't seem to find any references on that, but by observing values for those properties they get assigned to true by default.

like image 444
code-jaff Avatar asked Jun 15 '16 07:06

code-jaff


People also ask

How do I pass a prop to a component?

To pass props, add them to the JSX, just like you would with HTML attributes. To read props, use the function Avatar({ person, size }) destructuring syntax.

How do you pass non string props in React?

To pass a number as props to a component in React, wrap the number in curly braces, e.g. <Child num={42} /> . All props you pass to a component that are not of type string have to be wrapped in curly braces.

Can we pass props to functional component?

Of course, you can still pass props from parent to child with functional components but the big difference is that you'll need to declare them in your functional component callback just as you would with any other type of function. Now you can access those props.

Can you pass props to a class component?

As said, there is no way passing props from a child to a parent component. But you can always pass functions from parent to child components, whereas the child components make use of these functions and the functions may change the state in a parent component above.


1 Answers

What you are passing is interpreted by the compiler as a boolean attribute. This is also true for when writing pure HTML; attributes without values are interpreted as a boolean true. Since JSX is a syntactic-sugar for writing HTML, it makes sense that it has the same behavior.

The official React documentation has the following:

Boolean Attributes

This often comes up when using HTML form elements, with attributes like disabled, required, checked and readOnly. Omitting the value of an attribute causes JSX to treat it as true. To pass false an attribute expression must be used.

// These two are equivalent in JSX for disabling a button

<input type="button" disabled />; <input type="button" disabled={true} />; 

// And these two are equivalent in JSX for not disabling a button

<input type="button" />; <input type="button" disabled={false} />; 

Example

JSX:

<div>   <Component autoHeight />   <AnotherComponent autoHeight={null} /> </div> 

JS:

React.createElement(   "div",   null,   React.createElement(Component, { autoHeight: true }),   React.createElement(AnotherComponent, { autoHeight: null }) ); 

Check the babel demo of this, here.


Solution

As ctrlplusb stated, if you want to pass an "empty prop" you can simply give it the value of null or even undefined.

So you could do:

<SomeComponent disableHeight={null}>     {({width}) => (         <AnotherComponent             autoHeight={null}             width={width}             height={300}             {...otherProps}         />     )} </SomeComponent> 

Though I will note that passing it as undefined is probably entirely unnecessary because reading this.props.autoHeight from AnotherComponent will always give you undefined, regardless if you explicitly passed it as autoHeight={undefined} or not at all. Passing null is probably better in such cases since you are explicitly passing the prop by stating that it has the value of... "no value" (i.e null).

like image 91
Chris Avatar answered Sep 28 '22 02:09

Chris