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.

Preact VNode incompatible with React because it expects `constructor` to be `undefined`.

See original GitHub issue

Context

I’m building a vanilla JavaScript library that uses Preact for its stage management and accepts JSX elements for customizing the UI.

JSX elements coming from Preact work as expected. However, React elements are ignored.

Why

Preact considers a valid JSX element to have a constructor key set to undefined:

https://github.com/preactjs/preact/blob/3d2edb6f3cd59b40f8021f36948bae90bf760683/src/create-element.js#L83-L89

I’m not sure whether this is intentional to have this check now, it might have been here for legacy reasons (prior to Preact X).

For React elements to be compatible with the library I’m building, I need to loop over the children and to add the constructor key set to undefined.

 {
+  constructor: undefined,
   props: {
     children: "Hello"
   }
 }

Why is constructor necessary for a Preact element to be valid?

Preview

Here is a reproduction of the issue, with a user-land fix.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
marvinhagemeistercommented, Nov 13, 2019

This is a new safety guard that was introduced with Preact X. It guards against a possible XSS injection vulnerability, where a bad actor might pass custom vnodes and thus would be able to take control of props.dangerouslySetInnerHTML.

const BAD = {
  type: "script",
  props: {
    dangerouslySetInnerHTML: "alert('XSS')"
  }
};

render(<div>{BAD}</div>, container);

The fix ensures that no JSON structure can be passed as children.

EDIT: Note that compact works with react elements out of the box. It uses isValidElement under the hood.

1reaction
francoischalifourcommented, Dec 9, 2019

Thank you for the clarifications, this is highly appreciated.

I might turn the problem around to start from a new angle then: leveraging Preact only as state management and creating specialized renderers for different frameworks. I’m still trying to figure out if that’s a legit use of Preact. At this point what I’m doing is experimental and I’m trying multiple approaches.

I joined the Preact Slack workspace which might be more appropriate for future related questions.

Since the original issue is answered, feel free to close it. Thank you again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preact-compat returning component <undefined></undefined>
Preact -compat returning component <undefined></undefined> ... Too bad no error is thrown or displayed in the console. I'm not sure what I could...
Read more >
Preact - Releases
This release contains a fix to increase compatibility with next.js that ensures that the error overlay will show up. Bug Fixes. Emit error...
Read more >
storybookjs/storybook (Raised $170.00) - Issuehunt
The UI component explorer. Develop, document, & test for React, Vue, Angular, Ember, Web Components, & more! Owners:.
Read more >
cannot set properties of undefined (setting 'styles') - You.com
I am trying to configure a NextJS 12 project with custom Ant Design and according to some examples that I have found, I...
Read more >
Differences to React - Preact
The reason Preact does not attempt to include every single feature of React is in order to remain small and focused - otherwise...
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