【问题标题】:Getting error while showing hiding React Table columns - React JS显示隐藏 React Table 列时出错 - React JS
【发布时间】:2019-05-09 10:56:36
【问题描述】:

我正在研究 React Table。我基本上是 React 的初学者。我有一个仪表板页面,其中显示了一个包含 8 列的 React 表。我有一个自定义按钮,它将打开一个弹出页面,这个弹出页面有 8 个复选框允许我显示/隐藏那些 React 列。最初,此弹出页面中的所有复选框都设置为 true。当我取消选中某个列时,该列会被禁用。

最后有图看看我在做什么。

我将使用此逻辑来显示隐藏列(两天前我问过这个问题)- How to show and hide some columns on React Table?

React Table 数据是这样的

const columns = [
 {
    Header: 'Column 1',
    accessor: 'firstName',
  },
  {
    Header: 'Column 2',
    accessor: 'firstName',
  },
  {
    Header: 'Column 3',
    accessor: 'firstName',
  },
  {
    Header: 'Column 4',
    accessor: 'firstName',
  },
  {
    Header: 'Column 5',
    accessor: 'firstName',
  },
  {
    Header: 'Column 6',
    accessor: 'firstName',
  },
  {
    Header: 'Column 7',
    accessor: 'firstName'
  },
  {
    Header: 'Column 8',
    accessor: 'firstName',
  }
];

仪表板页面的开始

class Dashboard extends Component {
  constructor(props) {
    super(props);
    this.state = {
          filterState: {},
          searchText: '',
          isFilterOpen: false,
          isCustomizedOpen: false,
          isFiltered: false,
          isSearched: false,
          searchedTableData: [],
          filteredTableData: [],
        };
         this.handleCustClickinv = this.handleCustClickinv.bind(this);
    }

这是我在仪表板页面的渲染功能中用于显示自定义按钮的代码(这是写在父仪表板页面中的)

{this.state.isCustomizedOpen && <CustomizedView
          filterState={this.state.filterState}
          applyFilter={(values, clear) => { this.applyFilters(values, clear); }}
        />}

这是自定义按钮的代码(写在父仪表板页面中)

<div className="custom-div-dashboard" onClick={() => { this.handleCustClickinv(); }}>
            <div className='customize-view-dashboard'>Customized View </div>
          </div>

这是处理点击自定义按钮的功能(这写在父仪表板页面中)

handleFilterClickinv() {
    if(this.state.isCustomizedOpen) {
      this.setState({ isCustomizedOpen: false });
    }
    const currentState = this.state.isFilterOpen;
    this.setState({ isFilterOpen: !currentState });
  }

这是我的整个弹出页面,其中有 8 个复选框

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { ActionCreators } from '../../../actions';
import './enqCustomizedView.scss';
import ButtonComponent from '../../shared/button/ButtonComponent';
import { CheckBox } from '../../shared/chkbox/CheckBox';

class CustomizedView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [
        { id: 1, value: 'Column 1', isChecked: true },
        { id: 2, value: 'Column 2', isChecked: true },
        { id: 3, value: 'Column 3', isChecked: true },
        { id: 4, value: 'Column 4', isChecked: true },
        { id: 5, value: 'Column 5', isChecked: true },
        { id: 6, value: 'Column 6', isChecked: true },
        { id: 7, value: 'Column 7', isChecked: true },
        { id: 8, value: 'Column 8', isChecked: true },
      ]
    };
      this.handleCheckChildElement = this.handleCheckChildElement.bind(this);
  }

  handleClick() {
    this.setState({ isChecked: !this.state.isChecked });
  }

  handleCheckChildElement(event) {
    //let items = this.state.items;
    let { items } = this.state;
    items.forEach(items = () => {
      if(items.value === event.target.value) {
        items.isChecked = event.target.checked;
      }
    });
    this.setState({ items });
    const column1checked = items[0].isChecked;
    console.log('column1checked ' + column1checked);
    const column2checked = items[1].isChecked;
    console.log('column2checked ' + column2checked);
    const column3checked = items[2].isChecked;
    console.log('column3checked ' + column3checked);
    const column4checked = items[3].isChecked;
    console.log('column4checked ' + column4checked);
    const column5checked = items[4].isChecked;
    console.log('column5checked ' + column5checked);
    const column6checked = items[5].isChecked;
    console.log('column6checked ' + column6checked);
    const column7checked = items[6].isChecked;
    console.log('column7checked ' + column7checked);
    const column8checked = items[7].isChecked;
    console.log('column8checked ' + column8checked);
  }

  render() {
    return (
      <div className='popup-page-custom' >
        <div className='bottomBar'>
          <ButtonComponent
            text='Apply'
            className='activeButton filterMargin'
            width='100'
            display='inline-block'
            onClick={() => { this.props.applyFilter(this.state, false); }}
          />
          <ButtonComponent
            text='Clear Filter'
            className='greyedButton clear-filter'
            width='100'
            display='block'
            marginTop='60'
            onClick={() => { this.props.applyFilter(this.state, true); }}
          />
        </div>
        <div>
          <div className='data-points-text'>
            <span> Columns </span>
          </div>
          <div className="App">
            <ul>
              {
                this.state.items.map((item, i) => {
                  return (<div key={i} ><CheckBox handleCheckChildElement={this.handleCheckChildElement} {...item} /></div>);
                })
              }
            </ul>
          </div>
        </div>
      </div>
    );
  }
}

CustomizedView.propTypes = {
  applyFilter: PropTypes.func.isRequired
};

CustomizedView.defaultProps = {
};

function mapStateToProps(state) {
  return {
    auth: state.auth
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators(ActionCreators, dispatch);
}

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

最终这是我的复选框页面

import React from 'react';
import PropTypes from 'prop-types';

export const CheckBox = (props) => {
  // super(props);
  return (
    <li>
      <input key={props.id} onClick={props.handleCheckChildElement} type="checkbox" checked={props.isChecked} value={props.value} /> {props.value}
    </li>
  );
};

CheckBox.propTypes = {
  id: PropTypes.string,
  handleCheckChildElement: PropTypes.func,
  isChecked: PropTypes.bool,
  value: PropTypes.string,
};

CheckBox.defaultProps = {
  id: '',
  handleCheckChildElement: null,
  isChecked: null,
  value: '',
};

export default CheckBox;

这是我的仪表板页面和弹出页面的非常基本(丑陋)的样式

这是我在取消选中复选框时在 Chrome 上遇到的错误

编辑 1 - 根据 Alireza Yadegari 的建议,我做了 1 行更改。但我仍然遇到 2 个错误。

编辑 2 - 根据 Alireza Yadegari 的建议,我应用了控制台。

【问题讨论】:

    标签: reactjs react-table


    【解决方案1】:

    你必须在你的构造函数中使用这段代码

    this.handleCheckChildElement = this.handleCheckChildElement.bind(this)
    

    【讨论】:

    • 我已经更新了问题 - 插入此行并尝试取消选中复选框后,我收到 2 个错误,以前的错误不存在,有 2 个新错误,请参阅最后 3 个屏幕截图
    • 当您尝试在您的方法中使用 console.log(items) 和 console.log(this.state) 时,您的结果是什么?
    • 我的意图是 - 有一个名为 show: true 的反应表属性。所有列的默认值为 true,但我们可以通过 show: false 将其设为 false。我正在关注这个问题(而不是在仪表板页面上制作复选框,我需要在弹出页面上制作复选框,并且我将每个常量的值存储在弹出页面中,我需要将这些常量值传输到 React Table Page (在父仪表板页面中再次导入和呈现。您可以检查并帮助我 - stackoverflow.com/questions/53638095
    • @Scooby 你有什么建议可以使用 let { items } = { ...this.state };而不是让 items = this.state.items;
    • 它给出了错误 - 堆栈上的某个人给了我这个解决方案 - stackoverflow.com/questions/53657372
    【解决方案2】:

    let { items } = { ...this.state };

    这是错误的...... 首先你将数组解构为对象然后说给我来自给定对象的项目道具......当然这是错误的

    const { items} = this.state;

    从状态中获取 items 道具

    最后......用 foreach 实现你的任务是个坏主意......

    CheckBox.defaultProps = {
       id: '', 
       handleCheckChildElement: null,
       isChecked: null, value: '', 
    };
    

    我不明白它的作用。你知道吗?

    【讨论】:

    • 得到这个错误 36:19 错误'items' is constant no-const-assign
    • 试试 let ` let {items} = this.state`
    • 请检查复选框页面的defaultprops和proptypes。我担心是否发生任何类型不匹配
    • 出现 2 个错误。错误 1 ​​- 未捕获的类型错误:无法读取未定义的属性“isChecked”。错误 2 - 未捕获的类型错误:this.state.items.map 不是函数。
    • id: PropTypes.string id 不是字符串它是像这样的数字替换 id: PropTypes.number
    【解决方案3】:

    我认为您的项目只是一个示例,不需要更多示例。 我只是说你的错误。

    首先,当您使用方法时,最好使用 'bind(this)' 来显示 react 方法所属的位置。

    其次,当您使用 state 时,react 只允许您在构造函数中更改它,并且无论您想在哪里更改它都必须使用“setState”方法(您可以在 react 文档中阅读这样做的原因)。

    最后,如果您的状态中有一个数组,您必须在某个临时对象中获取一个数组更改临时对象,然后使用“setState”方法应用更改。如果您有更多问题,请随时提问。

    【讨论】:

      猜你喜欢
      • 2020-08-31
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2022-07-18
      • 1970-01-01
      相关资源
      最近更新 更多