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

fix: hydration error in strict mode #652

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/grumpy-shrimps-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@craftjs/core': patch
---

Fix <Element /> hydration errror in react strict mode
61 changes: 27 additions & 34 deletions packages/core/src/nodes/Element.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ERROR_TOP_LEVEL_ELEMENT_NO_ID } from '@craftjs/utils';
import React, { useState } from 'react';
import React from 'react';
import invariant from 'tiny-invariant';

import { NodeElement } from './NodeElement';
Expand Down Expand Up @@ -40,45 +40,38 @@ export function Element<T extends React.ElementType>({
};

const { query, actions } = useInternalEditor();
const { node, inNodeContext } = useInternalNode((node) => ({
const { node } = useInternalNode((node) => ({
node: {
id: node.id,
data: node.data,
},
}));

const [linkedNodeId] = useState<NodeId | null>(() => {
invariant(!!id, ERROR_TOP_LEVEL_ELEMENT_NO_ID);
const { id: nodeId, data } = node;

if (inNodeContext) {
let linkedNodeId;

const existingNode =
data.linkedNodes &&
data.linkedNodes[id] &&
query.node(data.linkedNodes[id]).get();

// Render existing linked Node if it already exists (and is the same type as the JSX)
if (existingNode && existingNode.data.type === is) {
linkedNodeId = existingNode.id;
} else {
// otherwise, create and render a new linked Node
const linkedElement = React.createElement(
Element,
elementProps,
children
);

const tree = query.parseReactElement(linkedElement).toNodeTree();

linkedNodeId = tree.rootNodeId;
actions.history.ignore().addLinkedNodeFromTree(tree, nodeId, id);
}
return linkedNodeId;
}
invariant(!!id, ERROR_TOP_LEVEL_ELEMENT_NO_ID);

let linkedNodeId: string;

const { id: nodeId, data } = node;

const existingNode =
data.linkedNodes &&
data.linkedNodes[id] &&
query.node(data.linkedNodes[id]).get();

if (existingNode && existingNode.data.type === is) {
linkedNodeId = existingNode.id;
} else {
const linkedElement = React.createElement(Element, elementProps, children);

const tree = query.parseReactElement(linkedElement).toNodeTree();

linkedNodeId = tree.rootNodeId;
actions.history.ignore().addLinkedNodeFromTree(tree, nodeId, id);
}

if (!linkedNodeId) {
return null;
});
}

return linkedNodeId ? <NodeElement id={linkedNodeId} /> : null;
return <NodeElement id={linkedNodeId} />;
}
2 changes: 1 addition & 1 deletion packages/core/src/nodes/NodeElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const NodeElement: React.FC<React.PropsWithChildren<
>> = ({ id, render }) => {
return (
<NodeProvider id={id}>
<RenderNodeToElement render={render} />
<RenderNodeToElement id={id} render={render} />
</NodeProvider>
);
};
15 changes: 7 additions & 8 deletions packages/core/src/render/RenderNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ import React from 'react';
import { DefaultRender } from './DefaultRender';

import { useInternalEditor } from '../editor/useInternalEditor';
import { useInternalNode } from '../nodes/useInternalNode';

type RenderNodeToElementType = {
id: string;
render?: React.ReactElement;
};

export const RenderNodeToElement: React.FC<React.PropsWithChildren<
RenderNodeToElementType
>> = ({ render }) => {
const { hidden } = useInternalNode((node) => ({
hidden: node.data.hidden,
}));

const { onRender } = useInternalEditor((state) => ({
>> = ({ id, render }) => {
const { onRender, exists, isNodeHidden } = useInternalEditor((state) => ({
onRender: state.options.onRender,
exists: !!state.nodes[id],
isNodeHidden: state.nodes[id]?.data.hidden ?? false,
}));

// don't display the node since it's hidden
if (hidden) {
if (!exists || isNodeHidden) {
return null;
}

Expand Down
Loading