1

I am using react-data-grid and redux store to save the data user inputs on the grid/table. To access the current state, I am using props from the mapStateToProps method. But mapStateToProps is not providing the updated state. Refer: Line numbers 151 to 156 of: https://github.com/rupav/candis/blob/4bee19750b9449137ad85e99fdc38e1fdb52f996/candis/app/client/app/component/widget/DataEditor.jsx

props.row.update() on Line num 151 updates the redux-store (rows value), which is not gets reflected in the next call from mapDispatchToProps Line num. 156 . Although accessing store directly (without connect), provides me the updated state: Line num. 154

My ReactDataGrid component is setup as:

<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 })
        })
    }}
/>

And mapDispatchToProps and mapStateToProps setup is:

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)
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
rupav jain
  • 645
  • 1
  • 7
  • 13

0 Answers0