Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #183 #184

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ui/src/timespan/DoneTrackers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const DoneTrackers: React.FC<DoneTrackersProps> = ({addTagsToTracker}) =>
timeSpans={timeSpans}
addTagsToTracker={addTagsToTracker}
setHeight={setHeights}
height={heights[key] || 500}
/>
);
})}
Expand All @@ -125,14 +126,16 @@ export const DoneTrackers: React.FC<DoneTrackersProps> = ({addTagsToTracker}) =>
const DatedTimeSpans: React.FC<{
name: string;
setHeight: (cb: (height: Record<string, number>) => Record<string, number>) => void;
height: number;
timeSpans: TimeSpanProps[];
} & DoneTrackersProps> = ({name, timeSpans, addTagsToTracker, setHeight}) => {
} & DoneTrackersProps> = ({name, timeSpans, addTagsToTracker, setHeight, height}) => {
const ref = React.useRef<HTMLDivElement | null>();
React.useEffect(() => {
if (ref.current) {
setHeight((old) => ({...old, [name]: ref.current!.getBoundingClientRect().height}));
const currentHeight = ref.current && ref.current.getBoundingClientRect().height;
if (currentHeight != null && currentHeight !== height) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted this, to check outside of the setState. The react docs weren't clear it guaranteed that setState with the same value doesn't force a rerender.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does skip rerendering: https://react.dev/reference/react/useState#setstate-caveats

If the new value you provide is identical to the current state, as determined by an Object.is comparison, React will skip re-rendering the component and its children. This is an optimization. Although in some cases React may still need to call your component before skipping the children, it shouldn’t affect your code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although in some cases React may still need to call your component before skipping the children

Okay, then "reexecute" the compomennt instead of rerender, but yeah doesn't really matter as the new impl. behaves the same.

setHeight((old) => ({...old, [name]: currentHeight}));
}
}, [ref, name, setHeight]);
}, [ref, name, setHeight, height]);
return (
<div key={name} ref={(r) => (ref.current = r)}>
<Typography key={name} align="center" variant={'h5'}>
Expand Down
Loading