Skip to content

Commit c358cf3

Browse files
ankriprevwong
authored andcommitted
chore: add error when hooks is used outside of EditorContext (#286)
1 parent 21de65d commit c358cf3

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
QueryCallbacksFor,
55
wrapConnectorHooks,
66
EventHandlerConnectors,
7+
ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,
78
} from '@craftjs/utils';
89
import { useContext, useEffect, useMemo } from 'react';
10+
import invariant from 'tiny-invariant';
911

1012
import { EditorContext } from './EditorContext';
1113
import { QueryMethods } from './query';
@@ -34,6 +36,8 @@ export function useInternalEditor<C>(
3436
): useInternalEditorReturnType<C> {
3537
const handler = useEventHandler();
3638
const store = useContext(EditorContext);
39+
invariant(store, ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT);
40+
3741
const collected = useCollector(store, collector);
3842

3943
const connectorsUsage = 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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { wrapConnectorHooks } from '@craftjs/utils';
1+
import {
2+
wrapConnectorHooks,
3+
ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT,
4+
} from '@craftjs/utils';
25
import { useMemo, useContext } from 'react';
6+
import invariant from 'tiny-invariant';
37

48
import { NodeContext } from './NodeContext';
59

@@ -8,6 +12,8 @@ import { Node } from '../interfaces';
812

913
export function useInternalNode<S = null>(collect?: (node: Node) => S) {
1014
const context = useContext(NodeContext);
15+
invariant(context, ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT);
16+
1117
const { id, related } = context;
1218

1319
const {

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)