0

I have to use multiple dropdowns from semantic-ui-react in my project. They need to have different props. It looks like this

 <div className="wrapper">
        <img className="icon" src={iconA} alt="iconA"></img>
        <h1>A</h1>
        <Dropdown
          className="dropdown"
          search
          selection
          options={optionsA}
          placeholder="A"
          defaultValue="A"
          onChange={handleAChange}
        />
 </div>
 <div className="wrapper">
        <img className="icon" src={iconB} alt="iconB"></img>
        <h1>B</h1>
        <Dropdown
          className="dropdown"
          search
          selection
          options={optionsB}
          placeholder="B"
          defaultValue="B"
          onChange={handleBChange}
        />
 </div>

I want to refactor this and create a single component for this by pasing different props. Please guide me on how this can be refactored in the best way possible.

2 Answers 2

1

First, create your custom dropDown component and extract props using object destructuring, you can give deafult values to props there itself, but better use PropTypes for that.

const CustomDropDown = (props) => {
  const { 
    className,
    search,
    selection,
    options,
    placeholder,
    defaultValue,
    onChange
  } = props;

  return (
    <div className="wrapper">
      <img className="icon" src={iconA} alt="iconA"></img>
      <h1>A</h1>
      <Dropdown
        className={classname}
        search={search}
        selection={selection}
        options={optionsA}
        placeholder={placeholder}
        defaultValue={defaultValue}
        onChange={onChange}
      />
    </div>
  )
}

Now, call the component like this,

<CustomDropDown 
  className="dropdown"
  search
  selection
  options={optionsA}
  placeholder="A"
  defaultValue="A"
  onChange={handleAChange}
/>
Sign up to request clarification or add additional context in comments.

4 Comments

That helps! I think you missed the the img and h1 tags in the component. They need to have different props too!
yes, you can pass those too from <CustomDropDown /> component, and consume it.
There is also a special case where let's say I would want another p tag within div ClassName="wrapper" but only for a specific dropdown. Would that be possible?
you can use flag for that, i mean pass a boolean flag as a prop and display p tag based on that
0

You can do it as follows:

const DropDownWraper = ({
 header,
 options,
 onChange,
 iconProps,
 placeholde,
 defaultValue
 }) =>
    <div className="wrapper">
      <img
        className="icon"
        src={ iconProps.src }
        alt={ iconProps.alt } />
      <h1>{ header }</h1>
      <Dropdown
        search
        selection
        options={ options }
        className="dropdown"
        onChange={ onChange }
        placeholder={ placeholde } 
        defaultValue={ defaultValue } />
   </div>

2 Comments

The dropdown have different onChange functions
When you use the DropDownWraper component, you can pass handleAChange, handleBChange, etc. The same is true for everything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.