【问题标题】:Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>警告:在 <select> 上使用 `defaultValue` 或 `value` 属性,而不是在 <option> 上设置 `selected`
【发布时间】:2020-08-22 18:42:23
【问题描述】:

我有这个 Select 组件(使用 material-ui/core 4.9.13),我的控制台中有这个警告。这是一个渲染组件,这就是我所做的 {...otherProps} {...场地}。 我读过我可以使用类似的选项来解决这个问题,但它对我不起作用。有人可以帮我吗?

  <Select className={props.selectClassName}
                onChange={handleSelectChange} // does setValue on this field
                onOpen={handleOnOpen} // does something graphic
                displayEmpty={true}
                variant="outlined"
                {...otherProps}
                {...field}
                value={field.value || ''}
        >
            {OPTIONS_ARR
                .map((obj: { label: string, value: string, country?: string }, index: number) =>
                <option
                    className={`${classes.optionStyle} c-pointer`}
                    key={index}
                    value={obj.value}
                    defaultValue={field.value}
                >
                    {obj.label}
                </option>)}
        </Select>

在这里完成警告

Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.

【问题讨论】:

  • 请提供一个code sandbox 来重现您的问题。
  • 您有什么警告?有什么问题?
  • 更新了更多信息

标签: reactjs select material-ui warnings options


【解决方案1】:

试试这个:

 <Select className={props.selectClassName}
            onChange={handleSelectChange} // does setValue on this field
            onOpen={handleOnOpen} // does something graphic
            displayEmpty={true}
            variant="outlined"
            {...otherProps}
            {...field}
            value={field.value || ''}
    >
        {OPTIONS_ARR
            .map((obj: { label: string, value: string, country?: string }, index: number) =>
            <option
                className={`${classes.optionStyle} c-pointer`}
                key={index}
                value={obj.value}
            >
                {obj.label}
            </option>)}
    </Select>

【讨论】:

  • 您是否尝试过将 OPTIONS_ARR 中的值之一硬编码为 select 中的 value 属性?
  • 现在我尝试只给一个选项,但结果是一样的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 2016-08-10
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多