简体   繁体   English

在 React 中使用 2 个下拉菜单。 无法读取未定义的属性“地图”

[英]Using 2 dropdowns in React. Cannot read property 'map' of undefined

I'm having an issue with 2 dropdowns.我遇到了 2 个下拉菜单的问题。 I'm getting the following error: TypeError: Cannot read property 'map' of undefined .我收到以下错误: TypeError: Cannot read property 'map' of undefined

Both dropdowns should work at the same time depending if the user wants to select a value from one of them or wants to select values from both dropdowns.两个下拉菜单应同时工作,具体取决于用户是想从其中一个下拉菜单中获取 select 的值,还是想从两个下拉菜单中获取 select 的值。

This is how I have implemented my 2 Dropdowns and how they should work.这就是我实现 2 Dropdowns 的方式以及它们应该如何工作。 If I click the button I will get the error:如果我单击按钮,我将收到错误消息: 在此处输入图像描述

dropdown.js下拉.js

import * as actionTypes from '../actions';

const initialState = {
    selection: 'Ambos',
    dropdownValues: ['Chatbot', 'Agente'],

    selectionME: 'Todos',
    dropdownValuesME: ['Messenger', 'Web', 'WhatsApp']

};
//
const reducer = ( state = initialState, action ) => {
    if (typeof state === 'undefined') {
        return initialState
    }

    switch ( action.type ) {
        case actionTypes.UPDATE_SELECTION:
        {
            return {
                
                ...state,
                selection: action.dataSelected,
                dropdownValues: action.dropdownValues,

                selectionME: action.dataSelectedME,
                dropdownValuesME: action.dropdownValuesME
            }
        }
        default: {
         //statements;
         
         break;
        }
    }
    return state;
};


export default reducer;

DropdownSelect.js DropdownSelect.js

import React, { Component } from 'react';
import { ButtonDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
import {connect} from 'react-redux';
import * as actionTypes from '../../store/actions'

class DropdownSelect extends Component {

  constructor(props)
{
  super(props);
  console.log(props)
  this.state = {
    dropdownOpen: false,
    solved: this.props.selectData,
    dropdownValues: this.props.dropdownValues,

    dropdownOpenME: false,
    solvedME: this.props.selectDataME,
    dropdownValuesME: this.props.dropdownValuesME,
  }
}

componentDidMount() {
  if(this.props.onRef)
  this.props.onRef(this);

}

toggleDropdown = () => {
  this.setState({
    dropdownOpen: !this.state.dropdownOpen,
  });
}

toggleDropdown2 = () => {
  this.setState({
    dropdownOpenME: !this.state.dropdownOpenME,
  });
}

changeDropdownValue = async (event) => {
    const currentSolved = this.state.solved
    let newdropdownValues = this.state.dropdownValues.concat(currentSolved)
    newdropdownValues = newdropdownValues.filter(item => item !== event.target.innerText)

    await this.setState({
        dropdownOpen: !this.state.dropdownOpen,
        solved: event.target.innerText,
        dropdownValues: newdropdownValues,

    }, () => {
       this.props.onUpdate(this.state.solved, this.state.dropdownValues);
    });
}
changeDropdownValueME = async (event) => {
  const currentSolvedME = this.state.solvedME
  let newdropdownValuesME = this.state.dropdownValuesME.concat(currentSolvedME)
  newdropdownValuesME = newdropdownValuesME.filter(item2 => item2 !== event.target.innerText)

  await this.setState({
      dropdownOpenME: !this.state.dropdownOpenME,
      solvedME: event.target.innerText,
      dropdownValuesME: newdropdownValuesME

  }, () => {
     this.props.onUpdate(this.state.solvedME, this.state.dropdownValuesME);
  });
}

  render() {
    return (
    <>                              
      <ButtonDropdown isOpen={this.state.dropdownOpen} toggle={this.toggleDropdown}>
        <DropdownToggle caret>
        {this.state.solved}
        </DropdownToggle>
        <DropdownMenu>
          {this.state.dropdownValues.map(e => {return <DropdownItem key={e} onClick={this.changeDropdownValue}>{e}</DropdownItem>})}
        </DropdownMenu>
      </ButtonDropdown>
      
      <p className="text-uppercase text-left font-weight-bold -label">Tipo de atención</p>
      <ButtonDropdown isOpen={this.state.dropdownOpenME} toggle={this.toggleDropdown2}>
        <DropdownToggle caret>
        {this.state.solvedME}
        </DropdownToggle>
        <DropdownMenu>
          {this.state.dropdownValuesME.map(e => {return <DropdownItem key={e} onClick={this.changeDropdownValueME}>{e}</DropdownItem>})}
        </DropdownMenu>
      </ButtonDropdown>

      </>
    );
  }
}

const mapStateToProps = state => ({
  //selectData: state.dropDownReducer.selection || [],
  //dropdownValues: state.dropDownReducer.dropdownValues || ['Ambos', 'Chatbot', 'Agente'],
  selectData: state.dropDownReducer.selection,
  dropdownValues: state.dropDownReducer.dropdownValues,

  selectDataME: state.dropDownReducer.selectionME,
  dropdownValuesME: state.dropDownReducer.dropdownValuesME
});

const mapDispatchToProps = dispatch => {
    return {
        onUpdate: (selected, dropdownValues) => dispatch({type: actionTypes.UPDATE_SELECTION, dataSelected: selected, dropdownValues:dropdownValues})
          }
};

export default connect(mapStateToProps, mapDispatchToProps)(DropdownSelect);

hey Jorge try to add conditions before mapping the data嘿 Jorge 尝试在映射数据之前添加条件

        <DropdownMenu>
          {this.state.dropdownValues && this.state.dropdownValues.map(e => {return <DropdownItem key={e} onClick={this.changeDropdownValue}>{e}</DropdownItem>})}
        </DropdownMenu>

the same here和这里一样

{this.state.dropdownValuesME && this.state.dropdownValuesME.map(e => {return <DropdownItem key={e} onClick={this.changeDropdownValueME}>{e}</DropdownItem>})}

and I believe you should not assign the state directly from the props, you should create the state as an empty array then in the component did mount you should set the state with the new values我相信你不应该直接从道具分配 state,你应该将 state 创建为一个空数组,然后在组件中安装你应该使用新值设置 state

  this.state = {
    dropdownOpen: false,
    solved: this.props.selectData,
    dropdownValues: [],

    dropdownOpenME: false,
    solvedME: this.props.selectDataME,
    dropdownValuesME: [],
  }
}

componentDidMount() {
  if(this.props.onRef)
  this.props.onRef(this);
  this.setState(oldState => {...oldState, dropdownValues: this.props.dropdownValues ,dropdownValuesME: this.props.dropdownValuesME})
}

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

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