【问题标题】:Array Foreach vs MapReusable Floating Select Form Component : React and Bootstrap 5
【发布时间】:2022-12-01 19:08:29
【问题描述】:

Trying to create a reusable Floating-Select form component by passing an array but unable to display the options list. I'd appreciate your help. Thank you.

const options_A = [
        { label: "AA", value: "AA" },
        { label: "BB", value: "BB" },
        { label: "CC", value: "CC" },
  ]

const FormSelect = () => {

const [selectedVal, setselectedVal] = useState(null)
const handleChange = (val) =>{
    setselectedVal(val)
  }
  <FloatingSelect 
      value={selectedGroup} 
      options={options_A } 
      onChange={e => handleChange(e)}
      className="p-4"
  />

Below is my reusable FloatingSelect.tsx

export interface IStateManagerProps extends StateManagerProps {
    children?: ReactNode
    className?: string
    invalid?: boolean
    options: any
}

export default ({
    options,
    className,
    invalid,
    ...props
}: IStateManagerProps) => {
    
    return (
        <div className="form-floating">
            <select className="form-select">
            {/* <option value="d">dfdf</option> */}
                { 
                    options.forEach(element => {
                        <option value={element.value}>{element.label}</option>
                    })
                }
            </select>
            <label><i className="bx bx-meh-blank"></i>Select</label>
        </div>
    )
}

【问题讨论】:

    标签: javascript reactjs arrays bootstrap-4 bootstrap-5


    【解决方案1】:

    use map instead of forEach.

    options.map(element => ( 
          <option value={element.value}>{element.label}</option>
     ))
    

    【讨论】:

    • I already tried using map. Doesn't work.
    • make sure to remove the curly brackets
    猜你喜欢
    • 2018-09-27
    • 2021-02-01
    • 1970-01-01
    • 2018-09-19
    • 2022-01-03
    • 2020-08-10
    • 1970-01-01
    • 2022-12-02
    • 2016-02-24
    相关资源
    最近更新 更多