replaced useMemo with useCallback

main
Inga 🏳‍🌈 1 year ago
parent 28ee56b4f2
commit 6dab7c18cc
  1. 71
      src/routePlanner/index.tsx

@ -1,5 +1,5 @@
import { nanoid } from 'nanoid';
import { useMemo, useState } from 'preact/hooks';
import { useCallback, useState } from 'preact/hooks';
import { reorderElements } from '../shared/collections';
import { Coordinates } from '../shared/types';
import { ExportComponent } from './export';
@ -12,47 +12,46 @@ import './style.css';
export const RoutePlanner = () => {
const [markers, setMarkers] = useState<Marker[]>([]);
const onMarkersReorder = useMemo(
() =>
({ oldIndex, newIndex }: ReorderMarkersParams): void =>
setMarkers((oldMarkers) => {
console.log(
`Reordering markers: from ${oldIndex} to ${newIndex}`,
);
const newMarkers = reorderElements(
oldMarkers,
oldIndex,
newIndex,
);
return newMarkers.map((marker, i) => ({
...marker,
label: `${i + 1}`,
remove: () =>
const onMarkersReorder = useCallback(
({ oldIndex, newIndex }: ReorderMarkersParams): void =>
setMarkers((oldMarkers) => {
console.log(
`Reordering markers: from ${oldIndex} to ${newIndex}`,
);
const newMarkers = reorderElements(
oldMarkers,
oldIndex,
newIndex,
);
return newMarkers.map((marker, i) => ({
...marker,
label: `${i + 1}`,
remove: () =>
onMarkersReorder({
oldIndex: i,
newIndex: -1,
}),
...(i - 1 >= 0 && {
moveDown: () =>
onMarkersReorder({
oldIndex: i,
newIndex: -1,
newIndex: i - 1,
}),
...(i - 1 >= 0 && {
moveDown: () =>
onMarkersReorder({
oldIndex: i,
newIndex: i - 1,
}),
}),
...(i + 1 < newMarkers.length && {
moveUp: () =>
onMarkersReorder({
oldIndex: i,
newIndex: i + 1,
}),
}),
}));
}),
}),
...(i + 1 < newMarkers.length && {
moveUp: () =>
onMarkersReorder({
oldIndex: i,
newIndex: i + 1,
}),
}),
}));
}),
[],
);
const onMapClick = useMemo(
() => (coordinates: Coordinates) => {
const onMapClick = useCallback(
(coordinates: Coordinates) => {
setMarkers((markers) => [
...markers.slice(0, markers.length - 1),
...markers

Loading…
Cancel
Save