I want to render a list and want to make each item clickable.
But when I create a function inside renderItem then on every rerender its recreate. So how I can use clickhandler and memo on this code correctly ?
import { mockAvailableCarsData } from '../../utils/mockData';
import TableDashboardBox from '../TableDashboardBox/TableDashboardBox';
import { IAvailableCarsData } from './types';
const renderItem = (el: IAvailableCarsData) => {
const navigate = useNavigate();
return (
<>
<tr onClick={() => navigate(`/cars/${el.id}`} className="meetings-tr">
<td>{el.car} - {el.mark}</td>
<td>{el.fuel}</td>
</tr>
</>
)
};
function AvailableCars() {
return (
<TableDashboardBox<IAvailableCarsData>
data={mockAvailableCarsData}
headerData={['Auto', 'Reichweite']}
renderItem={renderItem}
keyExtractor={({id}) => id.toString()}
title="Available"
moreLinkURL="/cars"
createButtonText={null}
classNameBox="w-100 mt-24 av-cars-box"
/>
)
}
export default AvailableCars
2
Answers
Try this code. If you have any problem, feel free to reach out me.
Here is an annotated code.
in short
memo()
headers
andkeyExtractor
outside of the componentuseCallback()
to prevent rerenders