Preact VNode incompatible with React because it expects `constructor` to be `undefined`.
See original GitHub issueContext
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
:
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:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 ofprops.dangerouslySetInnerHTML
.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 usesisValidElement
under the hood.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.