简体   繁体   English

onChange 未在 react js 中触发

[英]onChange is not triggering in react js

I use the dropdown in react js app but onChange is not triggering我在 react js 应用程序中使用下拉菜单,但 onChange 没有触发

my code is我的代码是

import React from "react";
import PropTypes from "prop-types";
import Dropdown from 'react-dropdown';
const options = [
   { value: 'one', label: 'One' },
   { value: 'two', label: 'Two', className: 'myOptionClassName' },

 ];

 class WebDashboardPage extends React.Component {

  constructor(props) {
  super(props);
  this.state = {}
  }

 quan = (event)=> {
console.log("Option selected:");
 this.setState({ value: event.target.value });
};

render() {

return(
  <b><Dropdown className="dropdownCss" options={options} onChange={e => 
  this.quan(e.target.value)} /></b>
 );

}

when I click the items in dropdown it shows the error当我单击下拉列表中的项目时,它会显示错误

"TypeError: Cannot read property 'quan' of undefined" “类型错误:无法读取未定义的属性 'quan'”

I'm a newbie to react thanks in advance我是新手,提前感谢

There is no issue with the react-dropdown library. react-dropdown库没有问题。 Here is the code sandbox that I've set up and corrected OP's code.这是我设置并更正了 OP 代码的代码沙箱 It works.有用。

import React from "react";
import Dropdown from "react-dropdown";
import "react-dropdown/style.css";
const options = [
  { value: "one", label: "One" },
  { value: "two", label: "Two", className: "myOptionClassName" }
];
const defaultOption = options[0];
class WebDashboardPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedValue: ""
    };
  }

  quan = value => {
    this.setState({ selectedValue: value });
  };

  render() {
    return (
      <Dropdown options={options} value={defaultOption} onChange={this.quan} />
    );
  }
}

export default WebDashboardPage;  

You should just do it this way:你应该这样做:

<Dropdown className="dropdownCss" options={options} onChange={this.quan} />

Try this:尝试这个:

 class WebDashboardPage extends React.Component {

      constructor(props) {
      super(props);
      this.state = { value: '' }
      this.quan = this.quan.bind(this);
      }

     quan(event) {
     console.log("Option selected:");
     this.setState({ value: event.target.value });
    };

    render() {

    return(
      <div><Dropdown className="dropdownCss" options={options} onChange={this.quan} /></div>
     );

    }

It seems the issue is with the react-dropdown component itself.问题似乎出在react-dropdown组件本身。 You'll need to file an issue there.您需要在那里提交问题。

react-dropdown component might not be using this.props.onChange somewhere or might be using problematically. react-dropdown组件可能没有在某处使用this.props.onChange或者可能使用有问题。


Or, it's probably, the component requires value state which have not defined?或者,可能是组件需要尚未定义的value状态?

this.state = {
  value: ''
}

And was causing the issue?是造成问题的原因吗?

The dropdown dependency you are using does not fire onChange with event as argument instead it fires onChange with the selected option.Try changing您使用的下拉依赖项不会使用事件作为参数触发 onChange,而是使用所选选项触发 onChange。尝试更改

onChange={e => 
  this.quan(e.target.value)}

to

onChange={this.quan}

and change quan to并将 quan 更改为

quan = (selectedOption)=> {
 console.log("Option selected:"+selectedOption.value);
 this.setState({ value: selectedOption.value });
};

I have tried it on my machine and it wroks perfectly.我已经在我的机器上试过了,它工作得很好。 Also next important thing is don't put options the way you are doing instead put it on state.另外一个重要的事情是不要按照你正在做的方式放置选项,而是把它放在状态上。 my final code is我的最终代码是

class WebDashboardPage extends Component {

constructor(props) {
super(props);

const options = [
  {
    value: 'one',
    label: 'One'
  }, {
    value: 'two',
    label: 'Two',
    className: 'myOptionClassName'
  }
];

this.state = {options}
}

quan = (selectedOption) => {
console.log("Option selected:" + selectedOption.value);
this.setState({value: selectedOption.value});
};
render() {
return (<b><Dropdown className="dropdownCss" options={this.state.options} onChange={this.quan}/></b>);
}
}

I only did a little refactoring to the code.我只对代码做了一点重构。 The main change is in how Dropdown handles change.主要的变化在于Dropdown处理变化的方式。 When you pass in a function to handleChange , Dropdown calls the function internally and passes the selected object to it, so you all you needed to do was create a handler method that has one parameter which you'll use to update the state.当您将一个函数传递给handleChangeDropdown在内部调用该函数并将所选对象传递给它,因此您需要做的就是创建一个处理程序方法,该方法具有一个用于更新状态的参数。 I also set an initial state for value.我还为 value 设置了一个初始状态。 Here's is a demo https://codesandbox.io/s/4qz7n0okyw这是一个演示https://codesandbox.io/s/4qz7n0okyw

import React,  { Component, Fragment } from "react";
import ReactDOM from "react-dom";
import Dropdown from "react-dropdown";

const options = [
  { value: "one", label: "One" },
  { value: "two", label: "Two", className: "myOptionClassName" }
];

class WebDashboardPage extends Component {
  state = {
    value: {}
  };

  quan = value => {
    console.log("Option selected:", value);
    this.setState({ value });
  };

  render() {
    return (
      <Fragment>
        <Dropdown
          className="dropdownCss"
          options={options}
          onChange={this.quan}
        />
      </Fragment>
    );
  }
}

export default WebDashboardPage;

Change to onChange={this.quan} , also in the initial state you should state your this.state.value更改为onChange={this.quan} ,同样在初始状态,您应该声明您的 this.state.value

this.state = {
 value: ''
}

also try to learn it on html element, not on jsx也尝试在 html 元素上学习它,而不是在 jsx 上

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

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