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

feat(react): add useEditorForImmediateRender hook #4858

Closed
Closed
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
18 changes: 16 additions & 2 deletions packages/react/src/useEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {

import { Editor } from './Editor.js'

export const useEditor = (options: Partial<EditorOptions> = {}, deps: DependencyList = []) => {
const editorRef = useRef<Editor | null>(null)
const useEditorBase = (options: Partial<EditorOptions> = {}, deps: DependencyList = [], availableOnFirstRender = false) => {
const editorRef = useRef<Editor | null>(availableOnFirstRender ? new Editor(options) : null)
const [, forceUpdate] = useState({})

const {
Expand Down Expand Up @@ -124,3 +124,17 @@ export const useEditor = (options: Partial<EditorOptions> = {}, deps: Dependency

return editorRef.current
}

export const useEditor = (options: Partial<EditorOptions> = {}, deps: DependencyList = []): Editor | null => {
const editor = useEditorBase(options, deps)

return editor
}

// In case you want to render the editor immediately, you can use this hook.
// Caution: Does not work with SSR.
export const useEditorForImmediateRender = (options: Partial<EditorOptions> = {}, deps: DependencyList = []): Editor => {
const editor = useEditorBase(options, deps, true) as Editor
Copy link
Contributor

Choose a reason for hiding this comment

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

This a nice way to abstract the base editor logic unlike my PR. However, I highly recommend using memo instead of ref as the lifecycle becomes a lot more simpler (especially with editor cleanup)


return editor
}
Loading