首页 > 解决方案 > 在 React Native 中使用 '.splice()' 添加到数组

问题描述

我正在尝试.splice()在状态函数中使用 to 和一个对象到一个数组。我拥有的代码来自一个可以编译的记分员应用程序。

    state = {
         team1: 'Team 1',
         team2: 'Team 2',
         score1: 0,
         score2: 0,
         team1Stats: [0, 0, 0],
         team2Stats: [0, 0, 0],
         date: 'Enter Date Here',
         gameRecord: [
             ],
    }

    updateRanking = () => {
         this.state.gameRecord.splice(this.state.gameRecord.length, 0, {
         finalDate: this.state.date,
         team1FinalScore: this.state.score1,
         team2FinalScore: this.state.score2,
    })

当我运行代码时,应用程序会运行,但是当我调用时,.updateRanking()我收到一条错误消息:

错误不变违规:最小化 React 错误 #31

我必须改变什么?

标签: javascriptarraysreact-native

解决方案


推荐阅读