Skip to content

Commit 0320c78

Browse files
authored
chore: add error when hooks is used outside of EditorContext (#286)
* added Error when using useEditor or useNode outside of EditorContext * changed EditorContext default to null
1 parent d83d8a3 commit 0320c78

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

packages/core/src/editor/EditorContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import { createContext } from 'react';
33
import { EditorStore } from './store';
44

55
export type EditorContext = EditorStore;
6-
export const EditorContext = createContext<EditorContext>({} as EditorContext);
6+
export const EditorContext = createContext<EditorContext>(null);

packages/core/src/editor/useInternalEditor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
QueryCallbacksFor,
55
wrapConnectorHooks,
66
ChainableConnectors,
7+
ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,
78
} from '@craftjs/utils';
89
import { useContext, useMemo } from 'react';
10+
import invariant from 'tiny-invariant';
911

1012
import { EditorContext } from './EditorContext';
1113
import { QueryMethods } from './query';
@@ -35,8 +37,10 @@ export type useInternalEditorReturnType<C = null> = useCollectorReturnType<
3537
export function useInternalEditor<C>(
3638
collector?: EditorCollector<C>
3739
): useInternalEditorReturnType<C> {
38-
const handlers = useEventHandler();
3940
const store = useContext(EditorContext);
41+
invariant(store, ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT);
42+
43+
const handlers = useEventHandler();
4044
const collected = useCollector(store, collector);
4145

4246
const connectors = useMemo(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,
3+
ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT,
4+
} from '@craftjs/utils';
5+
import { render } from '@testing-library/react';
6+
import * as React from 'react';
7+
8+
import { useEditor } from '../useEditor';
9+
import { useNode } from '../useNode';
10+
11+
/**
12+
* since we are mocking useInternalEditor in useEditor.test.tsx we are using a dedicated test suite here
13+
*/
14+
describe('Using useEditor outside of EditorContext', () => {
15+
it('should throw an error when we use useEditor outside of <Editor />', () => {
16+
const TestComponent = () => {
17+
useEditor();
18+
return null;
19+
};
20+
21+
expect(() => {
22+
render(<TestComponent />);
23+
}).toThrowError(ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT);
24+
});
25+
});
26+
27+
describe('Using useNode outside of EditorContext', () => {
28+
it('should throw an error when we use useNode outside of <Editor />', () => {
29+
const TestComponent = () => {
30+
useNode();
31+
return null;
32+
};
33+
34+
expect(() => {
35+
render(<TestComponent />);
36+
}).toThrowError(ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT);
37+
});
38+
});

packages/core/src/nodes/useInternalNode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT } from '@craftjs/utils';
12
import { useMemo, useContext } from 'react';
3+
import invariant from 'tiny-invariant';
24

35
import { NodeContext, NodeContextType } from './NodeContext';
46

@@ -26,6 +28,8 @@ export function useInternalNode<S = null>(
2628
collect?: (node: Node) => S
2729
): useInternalNodeReturnType<S> {
2830
const context = useContext(NodeContext);
31+
invariant(context, ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT);
32+
2933
const { id, related, connectors } = context;
3034

3135
const { actions: EditorActions, query, ...collected } = useInternalEditor(

packages/utils/src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ export const ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER = `An Error occurred wh
4343
Available components in resolver: %availableComponents%
4444
4545
More info: https://craft.js.org/r/docs/api/editor#props`;
46+
47+
export const ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT = `You can only use useEditor in the context of <Editor />.
48+
49+
Please only use useEditor in components that are children of the <Editor /> component.`;
50+
51+
export const ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT = `You can only use useNode in the context of <Editor />.
52+
53+
Please only use useNode in components that are children of the <Editor /> component.`;

0 commit comments

Comments
 (0)