Skip to content

Commit

Permalink
state props before non-state props
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaGurevich committed Sep 11, 2023
1 parent 145b05a commit 2a19fb5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/hooks/useEditorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ export function useEditorView<T extends HTMLElement = HTMLElement>(

const editorProps = withFlushedDispatch(props, forceUpdate);

const stateProp = "state" in editorProps ? editorProps.state : undefined;

const state =
"defaultState" in editorProps
? editorProps.defaultState
: editorProps.state;

const nonStateProps = Object.fromEntries(
Object.entries(editorProps).filter(
([propName]) => propName !== "state" && propName !== "defaultState"
)
);

useLayoutEffect(() => {
return () => {
if (view) {
Expand Down Expand Up @@ -109,8 +117,12 @@ export function useEditorView<T extends HTMLElement = HTMLElement>(
}, [editorProps, mount, state, view]);

useLayoutEffect(() => {
view?.setProps(editorProps);
}, [view, editorProps]);
if (stateProp) view?.setProps({ state: stateProp });
}, [view, stateProp]);

useLayoutEffect(() => {
view?.setProps(nonStateProps);
}, [view, nonStateProps]);

return view;
}

0 comments on commit 2a19fb5

Please sign in to comment.