reactjs - 类型错误 : Cannot read property 'toLowerCase' of undefined in reactjs

标签 reactjs select antd

我使用的是antd DesignSelect。一切都很好,但当我用一些字母搜索时,我遇到了错误。

Unhandled Runtime Error

TypeError: Cannot read property 'toLowerCase' of undefined

enter image description here

组件代码:

    import React, { Component } from 'react';
    import Link from 'next/link';
    import Router from 'next/router';
    import { Form, Input, Select } from 'antd';
    const { Option } = Select;
    
    class FormCheckoutInformation extends Component {
        constructor(props) {
            super(props);
          
        }
    
     onChange(value) {
          console.log(`selected ${value}`);
        }
        
         onBlur() {
          console.log('blur');
        }
        
         onFocus() {
          console.log('focus');
        }
        
         onSearch(val) {
          console.log('search:', val);
        }
    
        render() {
     return (
            <Form className="ps-form--checkout">
               <div className="ps-form__content">
    
                  <div className="form-group">
                      <Form.Item>
                          {getFieldDecorator('district', {
                              rules: [
                                  {
                                      required: false,
                                      message:
                                          'Enter a district!',
                                  },
                              ],
                          })(
                              <Select
                                  showSearch
                                  style={{ width: 200 }}
                                  placeholder="Select a person"
                                  optionFilterProp="children"
                                  onChange={this.onChange}
                                  onFocus={this.onFocus}
                                  onBlur={this.onBlur}
                                  onSearch={this.onSearch}
                                  filterOption={(input, option) =>
                                  option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                  }
                              >
                                  <Option value="jack">Jack</Option>
                                  <Option value="lucy">Lucy</Option>
                                  <Option value="tom">Tom</Option>
                              </Select>,
                          )}
                      </Form.Item>
                </div>
             </div>
            </Form>
    );
    } 

我无法找出问题所在,因为我是 React.js 的新手。

最佳答案

您没有正确使用这些值。 ant design中对select进行排序的正确方法是:

filterOption={(input, option) =>
  option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}

您需要从option.props访问子项

关于reactjs - 类型错误 : Cannot read property 'toLowerCase' of undefined in reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63752331/

相关文章:

ReactJS Ant 设计选择组件模拟更改事件

reactjs - React-Redux - 未处理的拒绝(错误): Actions must be plain objects. 使用自定义中间件进行异步操作

javascript - 错误引用错误: React is not defined | Using React Components in Angular

css - 计算器屏幕上的大数字超出了边界

php - 评论者只能在他们的评论中删除评论选项

reactjs - 如何在 styled-components 的 GlobalStyles 中引入 Ant Design 的 css 样式?

javascript - 在现有数组中添加数组中的新对象

php - 选择名称相似的行

php - 选择行与最新日期的SQL PHP

reactjs - 如何使用 antd upload React 发送 multipart/form-data