繁体   English   中英

与redux形式一起使用时,react-select中未显示initialvalues

[英]initialvalues is not shown in react-select when using with redux form

我在选择字段中使用react-select。 我已经以这种格式将选项传递给它,因为react-select支持这种方式

export const statusList = [
  { value: 1, label: 'Active' },
  { value: 2, label: 'Paused' },
];

但是当我从api获取值时,就像这个client_id: 2

我现在应该如何预先填写选择字段? 我做了以下方法

const InputTextWithSearch = ({ children, input, options, isMulti, ...props }: Props) => {
  console.log('input value', input, options);
  return (
    <Wrapper className={props.className} style={{ width: '100%' }}>
      <Label>{props.label}</Label>
      {children}
      <Select
        {...props}
        clearable={props.clearable}
        isSearchable={false}
        options={options}
        {...input}
        onChange={input.onChange}
        onBlur={() => input.onBlur(input.value)}
        components={{
          DropdownIndicator: props.selectIcon || DropdownComponent,
        }}
        styles={SelectStyleSetting}
        isMulti={isMulti}
      />
    </Wrapper>
  );
};

initialvalues is like {client_id: 2}

<Field
   id="client-id"
   name="client_id"
   label="Client Name"
   placeholder="Select Client"
   className="input-field"
   component={GSearchSelect}
   options={clients}
   onChange={handleSelectChange('client_id')}
   disabled={!!company}
/>

从反应选择文档

value props - The value of the select; reflected by the selected option

One of <Object,Array<Object>,null,undefined
>

您必须传递作为选择组件的选择道具提供的对象数组一部分的对象,或者传递空选择时为null / undefined

具体来说,您应该通过

 { value: 2, label: 'Paused' },

作为值而不是{client_id: 2}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM