在ES6中不变地反应setState:无法将未定义或null转换为对象

时间:2018-10-23 21:08:43

标签: javascript reactjs

我正在尝试使用不应该改变状态但我不断收到错误的规则来设置状态。无法解决这个问题。

这是我的状态:

    Dim connString As String = ""
   connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + 
   TextBox2.Text 
    + "';Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
    Dim query As String = "SELECT * FROM [Servers$]"
    Dim conn As New OleDbConnection(connString)

    conn.Open()

    Dim cmd As New OleDbCommand(query, conn)
    Dim da As New OleDbDataAdapter(cmd)
    Dim ds As New DataSet()

    ds.Clear()
    DataGridView1.DataSource = ds
    DataGridView1.DataSource = Nothing



    da.Fill(ds)

    DataGridView1.AutoGenerateColumns = False
    DataGridView1.ColumnCount = 5
    DataGridView1.Columns(0).DataPropertyName = "INTERNAL USE ONLY"
    DataGridView1.Columns(1).DataPropertyName = "F3"
    DataGridView1.Columns(2).DataPropertyName = "F5"
    DataGridView1.Columns(3).DataPropertyName = "F12"
    DataGridView1.DataSource = ds.Tables(0)


    Dim rowindex As String
    Dim mtCell As Integer = 0
    Dim row As DataGridViewRow = New DataGridViewRow()

    For rowNo As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
        row = DataGridView1.Rows(rowNo)
        Try
            For j = 0 To row.Cells.Count - 2
                If row.Cells(j).Value Is Nothing OrElse row.Cells(j).Value Is DBNull.Value Then
                    mtCell += 1
                End If
            Next

            If mtCell = row.Cells.Count - 1 Then
                DataGridView1.Rows.RemoveAt(rowNo)

            End If
            mtCell = 0
        Catch ex As Exception
            Exit For
        End Try
    Next rowNo
    If RadioButton7.Checked Then
        For rowNo As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
            row = DataGridView1.Rows(rowNo)
            Try
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Production" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "UAT" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Pre-Production" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Development" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(0).Value = "Server Name" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Disaster Recovery" Then
                        mtCell += 1
                    End If
                Next
                If mtCell = row.Cells.Count - 1 Then
                    rowindex = row.Index.ToString()
                    DataGridView1.Rows.RemoveAt(rowindex)
                    DataGridView1.Rows.RemoveAt(0)
                End If
                mtCell = 0
            Catch ex As Exception
                Exit For
            End Try
        Next rowNo
    End If`

当组件挂载时,我会执行以下操作:

this.state = {
      jsonReturnedValue: null,
      fruit: [
        {
          id: 1,
          title: 'Orange',
          selected: false,
          key: 'fruit'
        },
        {
          id: 2,
          title: 'Grape',
          selected: false,
          key: 'fruit'
        },
        {
          id: 3,
          title: 'Pomegranate',
          selected: false,
          key: 'fruit'
        },
        {
          id: 4,
          title: 'Strawberry',
          selected: false,
          key: 'fruit'
        }
      ]
    }

这将返回错误: 未捕获(承诺)TypeError:无法将未定义或null转换为对象

以下作品

  componentDidMount() {
    fetch('http://127.0.0.1:8000/api/printing/postcards-printing')
      .then(response => response.json())
      .then(json => {
        this.setState({ jsonReturnedValue: [...this.state.jsonReturnedValue, json.printCategory.products] }, () => console.log(this.state));
      });
  }

但是,根据我所读的内容,正在工作的人会改变状态,这是一种不好的做法。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您正试图传播一个空的原语,这是不可能的。因此,请使用您的初始状态执行此操作:

this.state = {
  jsonReturnedValue: [],
  fruit: [
    {
      id: 1,
      title: 'Orange',
      selected: false,
      key: 'fruit'
    },
    {
      id: 2,
      title: 'Grape',
      selected: false,
      key: 'fruit'
    },
    {
      id: 3,
      title: 'Pomegranate',
      selected: false,
      key: 'fruit'
    },
    {
      id: 4,
      title: 'Strawberry',
      selected: false,
      key: 'fruit'
    }
  ]
};

还要注意,您将要执行此{ jsonReturnedValue: [...this.state.jsonReturnedValue, ...json.printCategory.products] }。请注意在数组的第二个索引中的分布。