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

When a component containing craft.js's Element is dragged onto the Frame, a warning appears #693

Open
Traveller23 opened this issue Aug 12, 2024 · 0 comments

Comments

@Traveller23
Copy link
Contributor

When a component containing craft.js's Element is dragged onto the Frame, the console displays the following warning:

1

Warning: Cannot update a component (UiTree) while rendering a different component (Ce). To locate the bad setState() call inside Ce, follow the stack trace as described in https://reactjs.org/link/setstate-in-render

This warning doesn't seem to cause any problems, but it's annoying.

The problematic code is as follows:

const [linkedNodeId] = useState<NodeId | null>(() => {
invariant(!!id, ERROR_TOP_LEVEL_ELEMENT_NO_ID);
const node = query.node(nodeId).get();
if (inNodeContext) {
const existingNode = node.data.linkedNodes[id]
? query.node(node.data.linkedNodes[id]).get()
: null;
// Render existing linked Node if it already exists (and is the same type as the JSX)
if (existingNode && existingNode.data.type === is) {
return existingNode.id;
}
// otherwise, create and render a new linked Node
const linkedElement = React.createElement(
Element,
elementProps,
children
);
const tree = query.parseReactElement(linkedElement).toNodeTree();
actions.history.ignore().addLinkedNodeFromTree(tree, nodeId, id);
return tree.rootNodeId;
}
return null;
});

According to the official description (https://react.dev/reference/react/useState#parameters), the initialState should be a pure function, i.e. you can't change the state in it.

Your environment

Software Version(s)
craft.js @craftjs/[email protected]
React 18.3.1
TypeScript 5.5.4
Browser Chrome 127.0.6533.100
npm/Yarn pnpm 9.4.0
Operating System Windows 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant