-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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: default to rendering the editor immediately, while staying backward compatible #5161
Open
nperez0111
wants to merge
28
commits into
main
Choose a base branch
from
fix/react-rerenders
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+607
−383
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…boration (#4976) * extension-collaboration: allow passing ySyncOptions * fixes import
Co-authored-by: Pepijn Hillemans <pepijn.hillemans@cm.com>
✅ Deploy Preview for tiptap-embed ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
nperez0111
commented
May 16, 2024
} from 'react' | ||
|
||
import { Editor } from './Editor.js' | ||
|
||
const isDev = process.env.NODE_ENV !== 'production' | ||
const isSSR = typeof window === 'undefined' | ||
const isNext = isSSR || Boolean(typeof window !== 'undefined' && (window as any).next) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the closest thing I could find to detecting that you are running Next.js (SSR or CSR)
2 tasks
4 tasks
nperez0111
force-pushed
the
fix/react-rerenders
branch
from
June 8, 2024 14:37
57d60ec
to
2ae6496
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes Overview
A third PR for performance of the useEditor hook 😅.
What I've done here is combine a few of the different approaches and added a bit of my own spin to it:
Prior art:
What I've done here is to take a spin on #4579 and base it on a
useSyncExternalStore
instead (for efficient re-rendering handled by react).While adding the performance optimization made by #4453 (re-using existing editor instances, and just updating their options if they change).
It is backward-compatible to people who use SSR (printing a warning and telling them they should set the new flag explicitly for forward-compatible but only in dev mode).
It is forward-compatible, where if you don't specify the new flag, people will automatically have the editor on first render.
If the flag is specified, you get better TS types since it is now guaranteed that you get an editor instance.
If you include the
select
function, you can effectively choose when the component should re-render by returning something different than selected before (inspired byuseSelector
in react-redux)If you include the
equalityFn
function, you can choose how to compare yourselect
function's return value (again inspired byuseSelector
in react-redux)Implementation Approach
I've introduced 3 new options to the
useEditor
hook (and subsequently the editor provider).immediatelyRender
which is to control the new vs. the old behaviorselect
which is to give the consumer fine-grained control over rendering by selecting the values they care about in re-rendering the editorequalityFn
which defines how to compare the values that the consumer generates with theselect
functionWhen not specifying the
immediateRender
value, if it is detected to be in SSR mode (i.e. no window object) it will be assumed to befalse
to not break Next.js users (but printing a message that they should be explicit about this), otherwise it will be assumed to betrue
reducing flicker in existing applications.Eventually, when we have confidence to do so (i.e. the next major version) we can throw an error instead and have Next.js users (and other ssr frameworks) have to explicitly opt out for their use case (or perhaps a new hook).
Testing Done
I tested in the demos using all possible options and in a next application here is a table for reference:
Testing this sort of thing in react can be a little complicated (since you need to control when react re-renders) but if someone knows of a way to test this I'm happy to add tests for it.
Verification Steps
Run the demos and set different values for
immediatelyRender
and in a Next.js app.Additional Notes
I'm unsure what version tiptap claims to be compatible with, and I know older versions do not have
useSyncExternalStore
but I also know thatreact-redux
uses a shim to make it compatible with older versions.Checklist
feat: Implement new feature
orchore(deps): Update dependencies
)Related Issues
#5001