【问题标题】:React setState error anyone help ? minified error反应 setState 错误有人帮忙吗?缩小误差
【发布时间】:2021-05-31 07:38:18
【问题描述】:

我有一个双列表框,但是当我单击左或右按钮时,项目的传输到另一侧没问题,但我在控制台中收到一条错误消息。当我清除 rightClick this.setState 错误消失但项目永远不会改变或转移到另一边。我必须做什么?我分析每个人都说 NODE_ENV=development 我尝试但不适合我的错误?我通常像这样描述 this.setState() 我定义 this.setState({}) 但我不能这样定义我再次收到错误消息有人帮助吗?

this.state = {
      role: ['admin' , 'user' , 'subscriber' , 'superadmin'],
      activeRole:['admin1'],
      selectedIndex : 0,
      selectedItem : ''
      
    };

 menuClick = (activeItem,index) =>{
    this.selectedIndex = index;
    this.selectedItem = activeItem;
    // activeItem 1. arrayda varsa düğmeyi kapat
    // yoksa diğer düüme"112453
      // console.log('activeItem' + activeItem);
      // console.log('index' + index);
  }

  rightClick = ()=>{
    console.log(this.selectedIndex);
    console.log(this.state.role);
    this.setState(
      this.state.role.splice(this.selectedIndex, 1),
      this.state.activeRole.push(this.selectedItem)
    );
    
  }
  leftClick = ()=>{
    console.log(this.selectedIndex);
    console.log(this.state.role);
    this.setState(
      this.state.activeRole.splice(this.selectedIndex, 1),
      this.state.role.push(this.selectedItem)
    );
    
  }

【问题讨论】:

标签: javascript arrays reactjs state


【解决方案1】:

如下更新你的函数

  rightClick = () => {
console.log(this.selectedIndex);
console.log(this.state.role);
this.setState({
  role: this.state.role.splice(this.selectedIndex, 1),
  activeRole: this.state.activeRole.push(this.selectedItem)
});

  }

  leftClick = () => {
console.log(this.selectedIndex);
console.log(this.state.role);
this.setState({
  activeRole: this.state.activeRole.splice(this.selectedIndex, 1),
  role: this.state.role.push(this.selectedItem)
});


 }

【讨论】:

  • 当我这样做时它不起作用兄弟,我收到一条错误消息,没有任何事情发生没有改变。错误:Users.react.js:114 Uncaught TypeError: _this3.state.activeRole.push is not a function
  • 你能不能做个沙盒,我会很容易解决的
猜你喜欢
  • 1970-01-01
  • 2018-10-29
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
相关资源
最近更新 更多