How to pass props without value to component

JavascriptReactjsReact Jsx

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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 > > ; > ; > > // And these two are equivalent in JSX for not disabling a button > > ; > ;


###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).

Solution 2 - Javascript

Yeah JSX sees properties without a = as being a boolean true.

One option is to simply set null values:

<Foo name="Bob" surname={null} />

You could also dynamically build a property bag via an object and only add the properties if required. For example:

render() {
  const propBag = {
    width: width,
    height: 300
  };
  if (someCondition) {
    propBag.something = 'bob';
  }
  
  return (
    <FooComponent {...propBag} {..otherProps} />
  );
}

Solution 3 - Javascript

TL;DR: Set empty string:

<Amp.AmpAccordion animate="">

Explanation

A copy+paste from the link above:

>Any attribute with an empty string will render into the DOM without a value.

> W3C relevant documentation: https://www.w3.org/TR/html5/syntax.html#elements-attributes

>

Empty attribute syntax
Just the attribute name. The value is implicitly the empty string.

In the following example, the disabled attribute is given with the empty attribute syntax:

<input disabled>

If an attribute using the empty attribute syntax is to be followed by another attribute, then there must be a space character separating the two.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncode-jaffView Question on Stackoverflow
Solution 1 - JavascriptChrisView Answer on Stackoverflow
Solution 2 - JavascriptctrlplusbView Answer on Stackoverflow
Solution 3 - JavascriptghashiView Answer on Stackoverflow