简体   繁体   English

无法更改反应选择字段的默认值

[英]Unable to change default value for react-select field

I'm using a redux-form and have a custom component that uses react-select .我正在使用redux-form并有一个使用react-select的自定义组件。

I have created dropdown that has prefilled options and also allows custom input into that dropdown box.我创建了具有预填充选项的下拉列表,还允许自定义输入到该下拉框中。 I am using the " creatable " componant from react-select.我正在使用 react-select 中的“ creatable ”组件。

I want to be able to have one of the options as the default value on first load, this option would be highlighted in the dropdown list as selected too.我希望能够在第一次加载时将其中一个选项作为默认值,此选项也将在下拉列表中突出显示为选中状态。 I have achieved this but the only problem is, if I choose a new value or create a new value it stays as the default value - although the json does change to the correct value.我已经实现了这一点,但唯一的问题是,如果我选择一个新值或创建一个新值,它将保持默认值 - 尽管 json 确实更改为正确的值。

Here is my codesandbox example - https://codesandbox.io/s/jvzko04ok3这是我的代码和框示例 - https://codesandbox.io/s/jvzko04ok3

In my example you will see that Jim is selected by default, but as soon as you change the select option to something else the json values change but the view does not.在我的示例中,您将看到默认情况下选择了 Jim,但是一旦您将选择选项更改为其他内容,json 值就会更改,但视图不会。 Have a feeling its a onBlur or onChange issue.感觉它是 onBlur 或 onChange 问题。

There has been version changes to the react-select docs over the years, a default value can be specified using value or defaultValue .多年来,react-select 文档的版本发生了变化,可以使用valuedefaultValue指定默认值。

  <CreatableSelect
    {...props}
    options={props.options}
    onChange={value => input.onChange(value)}
    onBlur={() => input.onBlur(input.value)}
    value={props.options[1]}
  />

This is because you've tied the value prop directly to props.options[1] , which doesn't change.这是因为您已经将value prop 直接绑定到props.options[1] ,它不会改变。 Better to tie it to a state variable that you default to the option, and have your onChange update the state variable.最好将它绑定到默认为选项的状态变量,并让 onChange 更新状态变量。

Inspired by the accepted answer of Steves.受到史蒂夫斯公认答案的启发。 I converted my stateless component into a class component and gave it a default state and handled the onChange.我将我的无状态组件转换为类组件,并给它一个默认状态并处理 onChange。

Here is a codesandbox for other that may need a solution - https://codesandbox.io/s/kyr9mwpr5这是其他可能需要解决方案的代码和框 - https://codesandbox.io/s/kyr9mwpr5

From this:由此:

const LastNameSelectInput = ({ input, ...props }) => (
  <CreatableSelect
    {...props}
    options={props.options}
    // menuIsOpen={true}
    onChange={value => input.onChange(value)}
    onBlur={() => input.onBlur(input.value)}
    value={props.options[1]}
  />
);

to this:对此:

  class LastNameSelectInput extends React.Component {
  constructor(props) {
    super(props);
  }

  state = { value: this.props.options[1] };

  render() {
    const { input, options } = this.props;
    return (
      <CreatableSelect
        options={options}
        // menuIsOpen={true}
        onChange={value => {
          let newValue = input.onChange(value);
          this.setState({ value: newValue });
        }}
        onBlur={() => input.onBlur(input.value)}
        value={this.state.value}
      />
    );
  }
}

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

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