简体   繁体   English

react-select onChange 在更改选项后不起作用

[英]react-select onChange not working after change options

I have a component using react-select, I get the options from props, on selct option I want to rerender the select with other options - it works, but onChange not triggered.我有一个使用 react-select 的组件,我从 props 中获取选项,在 selct 选项上,我想用其他选项重新渲染 select - 它可以工作,但未触发 onChange。

import Select from 'react-select';

onChange(option){ 
   dispatch(updateOptions());
}

render(){
  return 
    <Select
      options={this.props.options}
      onChange={this.onChange}
      value={this.state.text}
      menuIsOpen
    />
} 

Please have a look at this example , I have created two components one as a Functional Component and the other as Class Component .请看一下 这个例子,我创建了两个组件,一个是Functional Component ,另一个是Class Component

Hope this helps.希望这可以帮助。

I was made example and it's working?我是一个例子,它的工作? Let's try it on your code让我们在您的代码上尝试一下

import React, { Component } from 'react';
import Select from 'react-select';


export default class FixedOptions extends Component {
  state = {
    value: {},
  };

  constructor(props) {
    super(props);
    this.onChange = this.onChange.bind(this);
  }

  onChange(value) {
    console.log(value)
    this.setState({ value: value });
  }

  render() {
    return (
      <Select
        value={this.state.value}
        onChange={this.onChange}
        options={[{a: 1, b: 2}, {a: 2, b: 3}]}
      />
    );
  }
}

Try onClick it's working as of now.试试 onClick 它现在正在工作。

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

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