2

**Here is the cart imports and state

const Cart = ({ cart, setActiveTab, removeFromCart, updateQuantity }) => {
const [selectedRows , setSelectedRows] = useState([]);
const [toggleCleared, setToggleCleared] = useState(false);
const [products, setProducts] = useState(cart.products) }    


 const columns = [
    {
      name: 'Product Name',
      selector: 'name',
      sortable: true,
    },
    {
      name: 'Product Price',
      selector: 'price',
      sortable: true,
      right: true,
    },
    {
        name: 'Product Quantity',
        selector: 'quantity',
        sortable: true,
        right: true,
        cell: row => <Fragment><input type="number" className="form-control" value={row.quantity} onChange = {(e)=>updateQuantity( row.id,strong text e.target.value)}/></Fragment>
      },
      {
        name: 'Subtotal',
        selector: 'quantity',
        sortable: true,
        right: true,
        cell: row => <Fragment>{row.quantity * row.price}</Fragment>
      },
      {
        name: 'Actions',
        selector: 'quantity',
        sortable: true,
        right: true,
        cell: row => <Fragment><i className="fa fa-trash" aria-hidden="true" onClick = {(e)=>removeFromCart(row.id)}></i></Fragment>
      },
  ];
  const contextActions = useMemo(() => {
    const handleDelete = () => {


        selectedRows.forEach(item=>{
            console.log(item, 'item')
            removeFromCart(products, products.indexOf(item))
        });

        setProducts(products);
        setToggleCleared(!toggleCleared);
        console.log('handleDelete', products)
    };

    return <i className="fa fa-trash" key="delete" onClick={handleDelete} style={{ backgroundColor: 'red' }} ></i>;
  });

This code sets the selected rows when we select rows

function rowSelection (rows) =>{
  setSelectedRows(rows.selectedRows);
}



  return (     <DataTable
                        title="Cart Products"
                        columns={columns}
                        data={products}
                        selectableRows
                        onSelectedRowsChange={rowSelection}
                        clearSelectedRows={toggleCleared}
                        contextActions={contextActions} />

)

**here is the my cart component which works fine but it does not updates itself when the redux state changes.. i can see the redux state in console that it changes perfectly but datatable does not rerenders

Sami ullah
  • 21
  • 2
  • I think there's relevant info missing here. Like what `setSelectedRows` refers to, or how you're connecting the data table to the store – Jayce444 Feb 24 '20 at 04:52
  • const Cart = ({ cart, setActiveTab, removeFromCart, updateQuantity }) => { const [selectedRows, setSelectedRows] = useState([]); const [toggleCleared, setToggleCleared] = useState(false); const [products, setProducts] = useState(cart.products) const Cart = ({ cart, setActiveTab, removeFromCart, updateQuantity }) => { const [selectedRows, setSelectedRows] = useState([]); const [toggleCleared, setToggleCleared] = useState(false); const [products, setProducts] = useState(cart.products) – Sami ullah Feb 24 '20 at 05:14
  • i have added complete code please check – Sami ullah Feb 24 '20 at 05:28
  • removeFromCart(products, products.indexOf(item)) - what are you doing in this function – Kathirpandian K Feb 24 '20 at 05:44
  • i am removing a product from products array.. it removes from the products but the view does not rerenders – Sami ullah Feb 24 '20 at 05:45
  • is there any way to redraw datatable?? – Sami ullah Feb 24 '20 at 05:51

1 Answers1

1

const [products, setProducts] = useState(cart.products) } this will only works on initial rendering ( called initial state).useState will not call once the render is completed. so just pass the cart.products in to the datatable


function rowSelection (rows) =>{
  setSelectedRows(rows.selectedRows);
}



  return (     <DataTable
                        title="Cart Products"
                        columns={columns}
                        data={cart.products}  
                        selectableRows 
                        onSelectedRowsChange={rowSelection}
                        clearSelectedRows={toggleCleared}
                        contextActions={contextActions} /> )
Kathirpandian K
  • 305
  • 1
  • 2
  • 15