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 all useEffect usages #12659

Merged
merged 5 commits into from
Aug 8, 2024
Merged

Remove all useEffect usages #12659

merged 5 commits into from
Aug 8, 2024

Conversation

MichaReiser
Copy link
Member

Summary

The primary usage of effects is to interact with an external system. We don't have this. That's why this PR replaces all
useEffect usages with better suited hooks or removes them alltogether. See https://react.dev/learn/you-might-not-need-an-effect

Test Plan

  • Made changes
  • Reload the editor
  • Verified that changes are persisted
  • Changed the theme to dark mode and reloaded the editor. Theme was persisted

I wasn't able to test sharing. The wrangler server goes up to 100% CPU usage whenever I hit share with or without my changes...

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Aug 4, 2024
</React.StrictMode>,
);
})
.catch((error) => console.error("Failed to start playground", error));
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this mean that nothing on the page will render until after the data fetch?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct. But arguably, this is the more "correct" behavior in the sense that showing anything is kind of meaningless and risks the user starting to change the code only so that it gets overridden once the response comes in.

I think the "proper" solution would be to split our skeleton:

  • The skeleton renders the header, left, and right navigation. It embeds the editor area
  • Use a context to store the source and revision
  • Fetch the code in the editor area
  • Wrap the editor area in a suspense boundary

But that's a larger refactor than what I want to do right now.

Copy link
Member

@charliermarsh charliermarsh Aug 5, 2024

Choose a reason for hiding this comment

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

But, on main, we don't even show the editor pane until the source is loaded, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

We did show the editor. See https://github.com/astral-sh/ruff/pull/12659/files#r1704097956

Isn't that changing this is your main concern?

Copy link
Member

Choose a reason for hiding this comment

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

I thought we showed the chrome (header) but not the editable panes in the editor, just based on reading the code where we have that source ? ternary in the editor body.

Copy link
Member Author

Choose a reason for hiding this comment

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

ah yeah, that's correct.

Copy link
Member

Choose a reason for hiding this comment

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

I defer to you, I don't want to get in your way.

Copy link
Member Author

Choose a reason for hiding this comment

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

I extracted a Chrome component. It would have been nice if React.use weren't experimental :(

@MichaReiser MichaReiser added playground A playground-specific issue and removed internal An internal refactor or improvement labels Aug 5, 2024
@charliermarsh
Copy link
Member

I (or someone) should probably test sharing before we merge this.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

I do think we should test sharing before merging. I can try later today.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Following [You might not need an effect](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application), remove `useEffect` to initialize the application
and instead run it before calling into React.
@MichaReiser MichaReiser force-pushed the reduce-use-effect-usage branch from ef99d78 to 7ff695a Compare August 5, 2024 13:57
Same as for the application state. We can run the logic exactly once and this way, avoid the hook.
Not sure why I used an effect here in the first place. The effect can be replaced with `useMemo`
@MichaReiser MichaReiser force-pushed the reduce-use-effect-usage branch from 7ff695a to 5f01bac Compare August 5, 2024 14:02
);
}, [pythonSource, settings]);

if (initPromise.current == null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Too bad that React.use is experimental because we could then store the promise and have an inner component that is wrapped in a Suspense boundary and uses the promise. For now, this is a hacky version that doesn't show a loading indicator

</React.StrictMode>,
);
})
.catch((error) => console.error("Failed to start playground", error));
Copy link
Member Author

Choose a reason for hiding this comment

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

I extracted a Chrome component. It would have been nice if React.use weren't experimental :(

@MichaReiser
Copy link
Member Author

Okay, I had to accept or decline the telemetry option, then force kill all wrangler process to get it working. But sharing works

@MichaReiser MichaReiser merged commit f537335 into main Aug 8, 2024
20 checks passed
@MichaReiser MichaReiser deleted the reduce-use-effect-usage branch August 8, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
playground A playground-specific issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants