React-table not updating data #4965
-
What i need:
Facing Issue:
My code: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To resolve this issue, you can modify your code as follows:
const handleMenu = (cell: any) => {
console.log(cell, 'celldd');
setSelectedRowId(cell.row.id);
};
{
row.row.id === selectedRowId && <DropdownMenu />
} With these modifications, the This is the updated code. import React, { useState, useMemo, useEffect } from 'react';
import { useTable, useRowSelect, useSortBy, usePagination } from 'react-table';
//Components
import DropdownMenu from './DropdownMenu';
import Toast from '../Toast/Toast';
//Icons
import {
MenuVertical,
SortAscending,
SortDescending,
} from '@assets/icons/svgIcons';
//Styles
import './style.scss';
const Table = (props: any) => {
const { data, columns, setApproveSelected } = props;
const [selectedRowId, setSelectedRowId] = useState('');
const {
getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
page,
}: any = useTable(
{ columns, data },
useSortBy,
usePagination,
useRowSelect,
(hooks) => {
hooks.visibleColumns.push(() => {
return [
// ...rest of the columns
{
id: 'more',
Header: '',
Cell: (row: any) => (
<>
<div className='drop__icon'>
<img
src={MenuVertical}
alt='Visible'
className='__otherContent'
onClick={() => handleMenu(row)}
/>
{row.row.id === selectedRowId && <DropdownMenu />}
</div>
</>
),
accessor: 'more',
isSortable: false,
disableSortBy: true,
},
];
});
},
);
const handleMenu = (cell: any) => {
console.log(cell, 'celldd');
setSelectedRowId(cell.row.id);
};
// rest of the function
return (
<>
<Toast
text='Success, Student will be notified via email'
isSuccess={true}
/>
<div className='maniTableContainer'>
<div className='maniTableContainer __scrollView'>
<table
{...getTableProps()}
className='maniTableContainer __mainTable'
>
{/* rest of the table rendering */}
</table>
</div>
</div>
</>
);
};
export default Table; Please note that the modifications have been made inside the Let me know if you need any further assistance! |
Beta Was this translation helpful? Give feedback.
To resolve this issue, you can modify your code as follows:
Remove the
selectingMenu
state from the Table component and replace it with a new state calledselectedRowId
. Initialize it with an empty string.Update the
handleMenu
function to set theselectedRowId
state instead of theselectingMenu
state.more
column's Cell component to check if the row's id matches the selectedRowId state value.With these modifications, the
selectedRowId
state will be correctly updated when clicking the "more" icon…