Reactjs 反应-警告:在上使用'defaultValue'或'value'道具,而不是在上设置'selected'
如何解决此警告 警告:使用默认值或值道具,而不是设置为“打开” 以前有人问过这个问题,但解决方案对我没有帮助。我无法设置selected,因为此处的值来自Redux状态(如果存在)。如果redux状态没有值,它应该显示默认选项-选择性别 这是我的密码:Reactjs 反应-警告:在上使用'defaultValue'或'value'道具,而不是在上设置'selected',reactjs,Reactjs,如何解决此警告 警告:使用默认值或值道具,而不是设置为“打开” 以前有人问过这个问题,但解决方案对我没有帮助。我无法设置selected,因为此处的值来自Redux状态(如果存在)。如果redux状态没有值,它应该显示默认选项-选择性别 这是我的密码: import React, { Component } from 'react' import InputGender from '../../edit-profile/gender-input' ... ... <InputGender
import React, { Component } from 'react'
import InputGender from '../../edit-profile/gender-input'
...
...
<InputGender value={gender} change={this.change} />
这是输入性别
import React from 'react'
import PropTypes from 'prop-types'
import Select from '../others/input/select'
const InputGender = ({ value, change }) => (
<div className="edit_gender_div">
<Select
placeholder="Select option"
value={value}
valueChange={e => change('gender', e)}
className="edit_gender mb-2"
>
<option value="" disabled selected>
Select Gender
</option>
<option>Male</option>
<option>Female</option>
<option>Other</option>
</Select>
</div>
)
InputGender.propTypes = {
value: PropTypes.string.isRequired,
change: PropTypes.func.isRequired,
}
export default InputGender
从选项中删除选定属性。值道具将在react自动处理的选项中设置所选值
<option value="" disabled selected>
{/* remove selected attribute ^^ */}
应该是:
<option disabled>
你必须做出一些改变 如果要选择禁用选项或性别值以选择其他值,请发送 选项应该是这样的
<option value="" disabled>{/* no need of selected */}
Select Gender
</option>
如果要选择选项案例,需要将目标选项值写入依赖于select标记的值中,如下所示:
<select value="first">
<option value="first">First</option>
<option value="second">Second</option>
</select>
如我所知,如果您想在每个表单标记上捕获onChange钩子,那么您需要对每个表单的输入使用onChange attr。因此,请将其更改为:
<Select
placeholder="Select option"
value={value}
onChange={e => change('gender', e)}
className="edit_gender mb-2"
>
...