score:86
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
).
score:4
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.
score:10
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} />
);
}
Source: stackoverflow.com
Related Query
- How to pass props without value to component
- How to pass styles via props with styled components without having to create the component inside the component
- React.js - how to pass event handlers to deeply nested component without props drilling?
- How to pass props to a component which is being passed as value
- How to call Child function from Parent / Pass Props to a Parent component without using ref methods?
- How to pass input value to form onSubmit without using state in component that renders multiple forms?
- How can I pass data to a component without props in React?
- How to pass value to props from component and set state
- How to pass props to component changing just one value
- How to pass props to route without causing component to mount twice?
- How can I pass a value to a component that doesn't have props in order to test it?
- How to pass value through props to another component and error : `Use the defaultValue or value props on instead of setting selected on <option>`?
- React - How to pass props to a component passed as prop
- How to extend styled component without passing props to underlying DOM element?
- How to pass props to Screen component with a tab navigator?
- How to pass props to component inside a React Navigation navigator?
- How to pass props to a styled component in emotion? Using TypeScript
- How to pass function as props from functional parent component to child
- How to pass style props for a specific component in react-native
- How do I pass props a component rendered with Enzyme.js?
- How to pass an object value in a form's select field onChange (using React, without Redux)
- How to pass props to react component that wrapped in variable
- How to pass props of React component to styled component
- How to pass value from parent component to child component (react)
- How to pass props without rendering the component?
- How to use setFieldValue and pass the value as props between components
- How to pass input value from child to parent component react
- How to pass a value to a StaticQuery using props string?
- How to pass rest of props to react component while also having required props defined in an interface
- How to pass props to a component passed as props in React?
More Query from same tag
- Combine two json arrays into one json array where the data will be completely merged into one single Json in react js
- Using Javascript to class manipulation in ReactJs
- Why isn't Formik.setStatus updating the Status state in my form?
- Remove Label on Material-UI Select Text Field
- Eslint - what rule is responses for highlighting wrong/undefined methods in React components?
- How to disable button onclick in Reactjs?
- TypeError: Cannot read property 'data' of null react hooks
- Why I keep get error like unexpected token when follow React.js tutorial
- Konva - Unable to completely destroy stage in react when unmounting component
- How to fit the image in tablet mode
- React Hooks D3 and Isotope libraries owning state
- React: how to properly type the HOC component?
- How to set a js property in getDerivedStateFromProps?
- ReactJS: Argument of type unknown is not assignable to parameter of type SetStateAction<string> from filereader
- How to make a scrollable section inside div that fills in all the remaining space of said div
- Error deploying with Netlify-cli in GitLab CI: Cannot find module netlify-cli/scripts/postinstall.js’
- How can I display a constant function within my return method with React?
- React useState with Array of objects
- Doesn't remove the right item in react
- Loop inside React JSX
- Invalid Date from an object
- ReactJS passing input data to next screen/component
- How to render list items in reactjs
- Observable.timer is not triggered in React application
- React native (ios). "saveToCameraRoll" error - "PHPhotosErrorDomain error -1"
- React js : change css class based on condition
- remove circles from google map react
- How to access passed data using router from one component to another ReactJS
- Child component props not updating after parent's state changes
- Using React, how can I update multiple elements with different values?