javascript - 如何在选择上触发选择更改事件?

标签 javascript reactjs

在我的无状态组件 DebtType 中,我触发了“handleChangeDebtType” 在 onChange 事件上:

const DebtType = (options, handleChangeDebtType) => {
  console.log(options);
  return (
    <select onChange={handleChangeDebtType}>
      {options.options.map(option => {
        return <option>{option.label}</option>;
      })}
    </select>
  );
};

export default DebtType;

此 DebtType 组件在 MyContainer 中调用:

import React, { Component } from "react";
import DebtType from "./DebtType";
import mockOptions from "./mockData.json";
import ClearDebtType from "./ClearDebt";
import { reduxForm } from "redux-form";
import { connect } from "react-redux";

class MyContainer extends Component {
  handleChangeDebtType= () => {
    //save selected debttype to store
    console.log("handleChangeDebtType");
  }

  render() {
    return (
      <div>
        <DebtType
          options={mockOptions.DEBT_TYPE}
          handleChangeDebtType={this.handledChangeDebtType}
        />
        <ClearDebtType options={mockOptions.CLEARDEBT_TYPE} />
      </div>
    );
  }
}

const mapStateToProps = state => ({
  //selectedDebtType:
});

MyContainer = connect(
  mapStateToProps,
  undefined
)(MyContainer);

export default reduxForm({
  form: "simple" // a unique identifier for this form
})(MyContainer);

//export default ;

如何触发“handleChangeDebtType”事件?目前它没有开火。

最佳答案

更新了 handleChange DebtType 函数,您的 handleChange Debt Type 函数名称中的小拼写错误

   import React, { Component } from "react";
import DebtType from "./DebtType";
import mockOptions from "./mockData.json";
import ClearDebtType from "./ClearDebt";
import { reduxForm } from "redux-form";
import { connect } from "react-redux";

class MyContainer extends Component {
  handleChangeDebtType = () => {
    //save selected debttype to store
    console.log("handleChangeDebtType");
  };

  render() {
    return (
      <div>
        <DebtType
          options={mockOptions.DEBT_TYPE}
          handleChangeDebtType={() => {
            this.handleChangeDebtType();
          }}
        />
        <ClearDebtType options={mockOptions.CLEARDEBT_TYPE} />
      </div>
    );
  }
}

const mapStateToProps = state => ({
  //selectedDebtType:
});

MyContainer = connect(
  mapStateToProps,
  undefined
)(MyContainer);

export default reduxForm({
  form: "simple" // a unique identifier for this form
})(MyContainer);

//export default ;

解构组件属性

import React from "react";



const DebtType = ({options, handleChangeDebtType}) => {
  console.log(options);
  return (
    <select onChange={handleChangeDebtType}>
      {options.map(option => {
        return <option key={option.label}>{option.label}</option>;
      })}
    </select>
  );
};

export default DebtType;

关于javascript - 如何在选择上触发选择更改事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823937/

相关文章:

javascript - 将 html 附加到 jQuery 元素而不在 html 中运行脚本

javascript - jquery如何在运行时获取数据?

javascript - Google 表格图表样式

javascript - gatsby graphql 无法查询类型 "allTribeEvents"上的字段 "Query"

reactjs - 修改状态对象而不调用setState,而是调用对象方法

javascript - React router + redux 向后导航不调用 componentWillMount

php - 如何在 jquery、codeigniter 实时搜索结果中建立链接?

Javascript/jQuery : get height when max-height is set to 0

javascript - 使用 PDF-LIB 在 React 中渲染 PDF

javascript - Next.js,如何将表单提交到另一个页面?