小编典典

如何在react-select中设置默认值

reactjs

我在使用react-select时遇到问题。我使用redux表单,并且使我的react-select组件与redux表单兼容。这是代码:

const MySelect = props => (
    <Select
        {...props}
        value={props.input.value}
        onChange={value => props.input.onChange(value)}
        onBlur={() => props.input.onBlur(props.input.value)}
        options={props.options}
        placeholder={props.placeholder}
        selectedValue={props.selectedValue}
    />
);

这里是我如何渲染它:

<div className="select-box__container">
    <Field
    id="side"
    name="side"
    component={SelectInput}
    options={sideOptions}
    clearable={false}
    placeholder="Select Side"
    selectedValue={label: 'Any', value: 'Any'}
    />
</div>

但是问题是我的下拉菜单没有我希望的默认值。我做错了什么?有任何想法吗?


阅读 1632

收藏
2020-07-22

共1个答案

小编典典

我猜你需要这样的东西:

const MySelect = props => (
<Select
    {...props}
    value={props.options.filter(option => option.label === 'Some label')}
    onChange={value => props.input.onChange(value)}
    onBlur={() => props.input.onBlur(props.input.value)}
    options={props.options}
    placeholder={props.placeholder}
  />
);
2020-07-22