【问题标题】:Error in dropdown component using material ui v1.0 beta 26使用材料 ui v1.0 beta 26 的下拉组件出错
【发布时间】:2018-06-12 13:45:57
【问题描述】:

我正在使用 Material UI v1.0 beta.26,我遇到了下拉组件的问题,在这个新版本中,您必须使用 Select 组件和 MenuItem。

我的下拉列表在应用程序渲染时填充,但是当我从中选择任何选项时,我收到以下错误:

这是我的代码:

import React from 'react';
import Select from 'material-ui/Select';
import {MenuItem, MenuIcon} from 'material-ui/Menu';

//CONSTANTS
import {CREATE_LS_DISCOUNT_TYPE_DD} from './commons/constants';
import {CREATE_LS_OFFER_TYPE_DD} from './commons/constants';

import cr from '../styles/general.css';


export default class ExampleDropDown extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      DiscountTypeData: [],
      OfferTypeData: [],
      DiscountTypeState: '',
      OfferTypeState: ''
    };

    this.renderDiscountTypeOptions = this.renderDiscountTypeOptions.bind(this);
    this.renderOfferTypeOptions = this.renderOfferTypeOptions.bind(this);

    this.handleChangeDiscountType = this.handleChangeDiscountType.bind(this);
    this.handleChangeOfferType = this.handleChangeOfferType.bind(this);

  }

  componentDidMount() {

    fetch(CREATE_LS_DISCOUNT_TYPE_DD)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          DiscountTypeData: findResponse.discountTypes,
        });
      });
  }

  handleChangeDiscountType(event, index, value) {
    this.setState({ DiscountTypeState: (value)});

    fetch(CREATE_LS_OFFER_TYPE_DD)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          OfferTypeData: findResponse.offerTypes
        });
      });
  }

  handleChangeOfferType(event, index, value) {
    this.setState({ OfferTypeState: event.target.value });
  }

  renderDiscountTypeOptions() {
    return this.state.DiscountTypeData.map((dt) => {
      return (
        <MenuItem
          key={dt.id}
          value={dt.text}>
          {dt.text}
        </MenuItem>
      );
    });
  }

  renderOfferTypeOptions() {
    return this.state.OfferTypeData.map((dt) => {
      return (
        <MenuItem
          key={dt.offerTypeCode}
          value={dt.offerTypeDesc}>
          {dt.offerTypeDesc}
        </MenuItem>
      );
    });
  }

  render() {

    return (

      <div className={cr.container}>
        <div>
          <Select

            value={this.state.DiscountTypeState}
            onChange={this.handleChangeDiscountType}>
            {this.renderDiscountTypeOptions()}

          </Select>
        </div>
        <br/>
        <div>
          <Select

            value={this.state.OfferTypeState}
            onChange={this.handleChangeOfferType}>
            {this.renderOfferTypeOptions()}

          </Select>
        </div>
      </div>

    );
  }

}

在以下方法 (handleChangeDiscountType) 中,如果我将其保留为“this.setState({ DiscountTypeState: value})”,则在上面的屏幕截图中出现错误,但如果我更改该行这个 "this.setState({ DiscountTypeState: event.target.value}) 它有效,所以我想了解原因

  handleChangeDiscountType(event, index, value) {
    this.setState({ DiscountTypeState: value});

    fetch(CREATE_LS_OFFER_TYPE_DD + 1)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          OfferTypeData: findResponse.offerTypes
        });
      });
  }

我还想做的是获取我的选择的索引,以便将它传递给我的第二个 Web 服务调用,但我不知道该怎么做,在以前版本的 Material UI 中我只是放了“ index”并且可以工作,但在新版本中不起作用,所以我想知道一种添加该参数的新方法。

fetch(CREATE_LS_OFFER_TYPE_DD + PASS INDEX HERE)
  .then(Response => Response.json())
  .then(findResponse => {
    console.log(findResponse);

    this.setState({
      OfferTypeData: findResponse.offerTypes
    });
  });

我会很感激这方面的任何帮助..

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    提供给SelectonChange 处理程序是用一个丰富了valuename 的目标调用的,因此您需要从event.target 中提取值:

    handleChangeDiscountType(event) {
      const {
        DiscountTypeData
      } = this.state;
    
      // you're using the text property as the value, but you should probably use its id
      // still, here's how you'd find the item using the selected item's value
      const selectedDiscount = DiscountTypeData.filter(
        discount => discount.text === event.target.value,
      );
    
      // use a templated literal to specify the endpoint with the selected item's id
      fetch(`${CREATE_LS_OFFER_TYPE_DD}/${selectedDiscount.id}`)
        .then(Response => Response.json())
        .then(findResponse => {
          console.log(findResponse);
    
          this.setState({
            OfferTypeData: findResponse.offerTypes,
          });
        });
    }
    

    您的代码不工作的原因是因为onChange 没有使用第三个参数调用,所以您使用value 将状态设置为undefined

    有关详细信息,请参阅Selects demo

    【讨论】:

    • 感谢您的宝贵时间,肯,还有一个问题...正如您在 Fetch 方法中看到的那样,我添加了一个“1”,但这是硬编码的,在之前版本的材料 ui 中我只是添加索引作为参数,而不是 1 我放入索引,但如果我这样做,我会在我的 Web 服务 URL 中获取和未定义,如下所示:“localhost:8080/services/OfferType/getOfferTypesByDiscountTypeId/…”所以取决于索引是值结束,信息正在返回...
    • 首先,您使用设置为数组索引的键来呈现 MenuItem,这是一种反模式,因此您应该找到一些独特的东西来键这些项目。不过,如果你真的需要数组索引,你可以使用Array.prototype.findIndex来找到匹配event.target.value的项,但是如果数组中有重复项,这很脆弱。
    • 所以我这样做的方式不对?因为在示例中,每个下拉菜单上的选项都是硬编码的,所以我没有找到任何动态示例,你会建议什么让它正常。抱歉有这么多问题,但我是 React 和 Material UI 的新手
    • 视情况而定。您从哪里获取下拉列表的数据?是否有任何唯一标识符可以用作密钥?如果是这样,请使用它。
    • 我更新了代码和我使用密钥的方式,数据来自这样的网络服务“0:{id:1,文本:“折扣(磅)”}“所以现在在代码中我做了类似“key={dt.id}”的东西,但仍然不知道如何将该 id 或密钥传递给这一行“fetch(CREATE_LS_OFFER_TYPE_DD + 1)”,而不是对数字 1 进行编码。
    猜你喜欢
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2019-10-06
    • 2021-05-18
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多