This content originally appeared on DEV Community and was authored by Luis Puentes Vega
When working with components, sometimes we need to send one specific property just when matches specific condition as an example:
function MyComponent = () => {
return (
<Select
label="Countries"
options={}
required
/>
);
}
if we want to send required just when matches specific conditions, we can spread the properties
function MyComponent = () => {
return (
<Select
label="Countries"
options={}
{...(shouldBeRequired && { required })}
/>
);
}
This content originally appeared on DEV Community and was authored by Luis Puentes Vega
Luis Puentes Vega | Sciencx (2022-06-03T01:01:28+00:00) React – Add prop to component if matches specific condition. Retrieved from https://www.scien.cx/2022/06/03/react-add-prop-to-component-if-matches-specific-condition/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.