search

Home  >  Q&A  >  body text

How to trigger render (with filter and pagination) on sort method in React?

This is my Sort.js style component:

<SortWrapper>
    <SortText>Sort By</SortText>
    <SortSelect onChange={onSelectChange}>
      <SortOption value="vehicleMake">Vehicle make</SortOption>
      <SortOption value="vehicleModel">Vehicle model</SortOption>
    </SortSelect>
  </SortWrapper>

This is the function in App.js where the sorting component is rendered

<Sort onSelectChange={onSelectChange} />

This is a function:

const onSelectChange = (e) => {
const value = e.target.value;

if (value === "VehicleMake")
  vehicles.sort((a, b) => a.vehicleMake.localeCompare(b.vehicleMake));
else if (value === "VehicleModel") {
  vehicles.sort((a, b) => a.vehicleModel.localeCompare(b.vehicleModel));
} else {
  vehicles.sort((a, b) => a.id - b.id);
}

"Vehicles" is an array of objects that I get from the Firestore database.

Whenever I change the value of the SortOption, it does not automatically sort the array of objects, but when I click on page 2 in the pagination, and then click on page 1, it sorts it.

This is my grid component:

<Grid>
      {vehiclesData.map((vehicle) => (
        <VehicleCard
          id={vehicle.id}
          key={vehicle.id}
          ImageURL={vehicle.ImageURL}
          vehicleMake={vehicle.vehicleMake}
          vehicleModel={vehicle.vehicleModel}
          selectVehicle={() => setSelectedVehicle(vehicle)}
          deleteHandler={() => deleteHandler(vehicle.id)}
        />
      ))}
    </Grid>

This is my filter function:

const vehiclesData = useMemo(() => {
let computedVehicles = vehicles;

if (searchVehicle) {
  computedVehicles = computedVehicles.filter((vehicle) =>
    vehicle.vehicleMake.toLowerCase().includes(searchVehicle.toLowerCase())
  );
}

setTotalVehicles(computedVehicles.length);

return computedVehicles.slice(
  (currentPage - 1) * vehiclesPerPage,
  (currentPage - 1) * vehiclesPerPage + vehiclesPerPage
);

As I wrote before, I want my vehicles to render automatically by make or model, not when I click on the page number in the pagination.

P粉970736384P粉970736384228 days ago2401

reply all(1)I'll reply

  • P粉790819727

    P粉7908197272024-04-07 15:43:05

    Your sorting method should be like this

    const onSelectChange = (e) => {
    let temVehicles = [...vehicles]
    const value = e.target.value;
    
    if (value === "VehicleMake")
      temVehicles .sort((a, b) => a.vehicleMake.localeCompare(b.vehicleMake));
    else if (value === "VehicleModel") {
      temVehicles .sort((a, b) => a.vehicleModel.localeCompare(b.vehicleModel));
    } else {
      temVehicles .sort((a, b) => a.id - b.id);
    }
    setVehicles(temVehicles )}

    Your filter function should be like this

    const vehiclesData = useMemo(() => {
    let computedVehicles = vehicles;
    
    if (searchVehicle) {
      computedVehicles = computedVehicles.filter((vehicle) =>
        vehicle.vehicleMake.toLowerCase().includes(searchVehicle.toLowerCase())
      );
    }
    
    setTotalVehicles(computedVehicles.length);
    
    return computedVehicles.slice(
      (currentPage - 1) * vehiclesPerPage,
      (currentPage - 1) * vehiclesPerPage + vehiclesPerPage
    );},[vehicles])

    Basically, once onSelectChange runs, you update the vehicle status and then usememo should run again, so we add the dependent vehicle to it

    reply
    0
  • Cancelreply