无法在 setState 上将隐藏列状态从 true 更改为 false

Cannot change hidden column state from true to false on setState

我正在使用 devexpress React table,您可以通过状态隐藏和显示列。

我想将 hidden 的状态从 true 更改为 false,但我收到了 Uncaught Invariant Violation 错误。

我试过使用 setState 但没有用。

class ResultsTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
        columns: [
            { name: 'agent', title: 'Agent' },
            { name: 'alertGroup', title: 'Alert Group', hidden:true },
            { name: 'manager', title: 'Manager', hidden:true }
            ],
        };
    }


    componentDidMount() {
        this.testAlert();
    }

    testAlert = () => {
        if (this.props.alertgroupColumn()) {
            this.setState({
                columns: [{ name: 'alertGroup', title: 'Alert Group', hidden:false }]
            })
        }
    }

隐藏应从 true 更改为 false。

我有另一种方法来更新你的状态

  class ResultsTable extends Component {
        constructor(props) {
            super(props);
            this.state = {
            columns: [
                { name: 'agent', title: 'Agent' },
                { name: 'alertGroup', title: 'Alert Group', hidden:true },
                { name: 'manager', title: 'Manager', hidden:true }
                ],
            };
        }


        componentDidMount() {
            this.testAlert();
        }

        testAlert = () => {
            if (this.props.alertgroupColumn()) {
//---- get the columns from state 
   let columns = JSON.parse(JSON.stringify(this.state.columns))
 columns[1].hidden = false
 this.setState({
      columns:columns 
    })  
                this.setState({
                    columns: [{ name: 'alertGroup', title: 'Alert Group', hidden:false }]
                })
            }
        }