React – Add prop to component if matches specific condition

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
/>


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » React – Add prop to component if matches specific condition." Luis Puentes Vega | Sciencx - Friday June 3, 2022, https://www.scien.cx/2022/06/03/react-add-prop-to-component-if-matches-specific-condition/
HARVARD
Luis Puentes Vega | Sciencx Friday June 3, 2022 » React – Add prop to component if matches specific condition., viewed ,<https://www.scien.cx/2022/06/03/react-add-prop-to-component-if-matches-specific-condition/>
VANCOUVER
Luis Puentes Vega | Sciencx - » React – Add prop to component if matches specific condition. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/03/react-add-prop-to-component-if-matches-specific-condition/
CHICAGO
" » React – Add prop to component if matches specific condition." Luis Puentes Vega | Sciencx - Accessed . https://www.scien.cx/2022/06/03/react-add-prop-to-component-if-matches-specific-condition/
IEEE
" » React – Add prop to component if matches specific condition." Luis Puentes Vega | Sciencx [Online]. Available: https://www.scien.cx/2022/06/03/react-add-prop-to-component-if-matches-specific-condition/. [Accessed: ]
rf:citation
» React – Add prop to component if matches specific condition | Luis Puentes Vega | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.