使用connect和mapStateToProps不提供更新的状态

时间:2018-08-05 05:29:46

标签: reactjs redux react-redux react-data-grid

我正在使用react-data-grid和redux存储将数据用户输入保存在表格/表上。要访问当前状态,我正在使用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存储(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)

0 个答案:

没有答案