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

Remove useEffect from TimeElapsed #3741

Merged
merged 1 commit into from
Apr 28, 2024
Merged

Remove useEffect from TimeElapsed #3741

merged 1 commit into from
Apr 28, 2024

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Apr 28, 2024

It's not needed and forces an unnecessary second render pass.

See https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes.

Test Plan

Waited a minute, verified timestamps get updated.

Copy link

render bot commented Apr 28, 2024

@@ -3,8 +3,6 @@ import React from 'react'
import {useTickEveryMinute} from '#/state/shell'
import {ago} from 'lib/strings/time'

// FIXME(dan): Figure out why the false positives
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is something unrelated about lint and types that's since been addressed.

Copy link

Old size New size Diff
6.44 MB 6.44 MB 30 B (0.00%)

Copy link
Member

@estrattonbailey estrattonbailey left a comment

Choose a reason for hiding this comment

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

Nice one

@gaearon gaearon merged commit 361d255 into main Apr 28, 2024
6 checks passed
@gaearon gaearon deleted the rm-fx branch April 28, 2024 21:37
React.useEffect(() => {
const [prevTick, setPrevTick] = React.useState(tick)
if (prevTick !== tick) {
setPrevTick(tick)
Copy link

@AhmedBaset AhmedBaset Apr 29, 2024

Choose a reason for hiding this comment

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

Isn't that (bad setState in render)?

I mean this error:

Warning: Cannot update a component (App) while rendering a different component (Watever). To locate the bad setState() call inside Watever, follow the stack trace as described in https://reactjs.org/link/setstate-in-render

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That error is about updating a different component. Updating the same component is ok if it's following this specific pattern: https://react.dev/reference/react/useState#storing-information-from-previous-renders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants