我正在使用react-data-grid和redux store来保存网格/表格上的数据用户输入 . 要访问当前状态,我使用的是mapStateToProps方法中的props . 但mapStateToProps未提供更新状态 . 参考:第151至156行:https://github.com/rupav/candis/blob/4bee19750b9449137ad85e99fdc38e1fdb52f996/candis/app/client/app/component/widget/DataEditor.jsx

Line num 151上的props.row.update()更新了redux-store( rows value ),它不会在mapDispatchToProps Line num. 156的下一次调用中反映出来 . 虽然直接访问商店(没有连接),但为我提供了更新的状态:Line num. 154

我的ReactDataGrid组件设置为:

<ReactDataGrid
     enableCellSelect={true}
              columns={props.columns}
            rowGetter={(index) => { return props.rows[index] }}
            rowsCount={props.rows.length}
         rowSelection={{
                showCheckbox: true,
           enableShiftSelect: true,
              onRowsSelected: (rows) => {
                rows.forEach((meta)  => {
                  props.row.select(meta.rowIdx, meta.row)
                })
              },
            onRowsDeselected: (rows) => {
              rows.forEach((meta)  => {
                props.row.deselect(meta.rowIdx, meta.row)
              })
            },
                    selectBy: { isSelectedKey: 'selected' }
         }}
    onGridRowsUpdated={({ fromRow, toRow, updated }) => {

      props.row.update(fromRow, toRow, updated).then(() => {

        console.log("props.rows are now!", props.rows)  // Still not updated
        // store.getState().dataEditor.rows  // this statement have updated rows, even without using the Promise.

        props.onChange({ columns: props.columns, rows: props.rows })
      })
    }}/>

mapDispatchToProps和mapStateToProps设置为:

const mapStateToProps = (state) => {
  const dataEditor    = state.dataEditor

  return {
       rows: dataEditor.rows,
    columns: dataEditor.columns
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    row: {
      update: (fromRow, toRow, updated) => new Promise ((resolve) => {
        const action = row.update(fromRow, toRow, updated)
        dispatch(action)
        resolve()
      }),
      insert: (position, meta)=> {
        const action = row.insert(position, meta)
        dispatch(action)
      },
      delete: (index, meta)=> {
        const action = row.delete(index, meta)
        dispatch(action)
      },
      select: (rowIdx, row) => {
        const action = row.select(rowIdx, row)
        dispatch(action)
      },
      deselect: (rowIdx, row) => {
        const action = row.deselect(rowIdx, row)
        dispatch(action)
      }
    },
    column: {
      insert: (position, meta)=> {
        const action = column.insert(position, meta)
        dispatch(action)
      },
      delete: (key)=> {
        const action = column.delete(key)
        dispatch(action)
      }
    },
    getResource: () => {
      const action = getResource()
      dispatch(action)
    }
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(DataEditor)