question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditionally wrapping `Element` causes an error, works without wrapping

See original GitHub issue

I’m trying to conditionally render either an Elemen or the actual dom node based on whether or not I’m editing or exporting. Currently my own solution for exporting the nodes to Next.js or HTML, otherwise it requires two separate copies of the same node, one for editing and one for exporting which I’m trying to avoid.

Copying the same Element implementation you have, I try to hand down the exact same information.

let isExporting = false;

export type Element<T extends React.ElementType> = {
  id?: NodeId;
  is?: T;
  custom?: Record<string, any>;
  children?: React.ReactNode;
  canvas?: boolean;
} & React.ComponentProps<T>;

export function Element<T extends React.ElementType>({
  id,
  children,
  ...props
}: Element<T>) {

  // const testElement1 = React.createElement(
  //   CraftElement,
  //   { id, is: "div", custom: props.custom, canvas: props.canvas },
  //   children
  // );

  const testElement2 = (
    <CraftElement id={id} {...props}>
      {children}
    </CraftElement>
  );

  return (isExporting
    ? (React.createElement(props.is, props.elementProps, children))
    : testElement2);

  // return (
  //   <CraftElement id={id} {...props}>
  //     {children}
  //   </CraftElement>
  // );
}

This actually works, but only as long as there are no children Elements, as having children causes

Error: Invariant failed: The component type specified for this node (Element) does not exist in the resolver

I’ve tried various implementations, all failing: directly supply the props, change the is to div to see if it’s a scope issue, etc.

Is there any way I can get this to work?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
wuichencommented, Apr 29, 2022

Hi I can confirm this error is still happening. It happens to me when I have < element >{children}< /element >

1reaction
prevwongcommented, Mar 8, 2022

Weird, would it be possible for you to create a codesandbox reproducing the behaviour?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the element wrapping its children based on a ...
vertical is true , I'm wrapping {props.children} and {help} in a span or a div. Is there a clean way to do this...
Read more >
How to conditionally wrap an element in React - Hackages Blog
To render a different wrapping element based on a condition, we can use this one line functional component and determine in our component...
Read more >
Wrapping and breaking text - CSS: Cascading Style Sheets
Wrapping and breaking text. This guide explains the various ways in which overflowing text can be managed in CSS.
Read more >
React conditional rendering: 9 methods with examples
An if...else block is the easiest way to solve the problem, but I'm sure you know this is not a good implementation. While...
Read more >
Conditional Rendering - React
You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found