Coder Social home page Coder Social logo

Comments (8)

Industrial avatar Industrial commented on June 3, 2024

It is solved by const X = ({ children }) => children[0]. Why can't it be an array, though?

from htm.

Industrial avatar Industrial commented on June 3, 2024

I'm running into many problems with libraries. Right now with Material UI.

example: Warning: Failed prop type: Invalid prop childrensupplied toGrid, expected a ReactNode. in Grid

from htm.

Industrial avatar Industrial commented on June 3, 2024

I tried http://recharts.org/en-US/ as an example and

  <${LineChart} width=${500} height=${300} data=${[0, 1, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 1, 0]}>
    <${XAxis} dataKey="name" />
    <${YAxis} />
    <${CartesianGrid} stroke="#eee" strokeDasharray="5 5" />
    <${Line} type="monotone" dataKey="uv" stroke="#8884d8" />
    <${Line} type="monotone" dataKey="pv" stroke="#82ca9d" />
  <//>

gives me

Uncaught Error: Objects are not valid as a React child (found: object with keys {nodeName, children, attributes, key}). If you meant to render a collection of children, use an array instead.

from htm.

developit avatar developit commented on June 3, 2024

@Industrial are you using Preact, or React? AFAIK Material-UI has issues with the current version of Preact, independent of HTM.

from htm.

Industrial avatar Industrial commented on June 3, 2024

That was with Preact, yeah.

from htm.

kilianc avatar kilianc commented on June 3, 2024

@developit I am getting the same error with React 16.7.0 and a basic hello world.

Objects are not valid as a React child (found: object with keys {type, props, children}). If you meant to render a collection of children, use an array instead.

import React from 'react'
import ReactDom from 'react-dom'

console.log(React.version)

function h (type, props, ...children) {
  return { type, props, children }
}

const html = htm.bind(h)

const HelloWorld = () => html`<h1>HI</h1>`
ReactDom.render(html`<${HelloWorld} />`, document.getElementById('main'))

Edit: I was missing the correct binding with createElement

const html = htm.bind(React.createElement)

from htm.

developit avatar developit commented on June 3, 2024

@kilianc with the binding corrected, can we close this issue?

@Industrial the current version of Preact does not support returning multiple roots from a component render, which is what returning props.children (an Array) would be. This is implemented in the upcoming Preact X, as it is backed by the same logic used for Fragments.

If you're just looking to use HTM with React, don't use the Preact version. Use this:

// htm-react.js
import htm from 'htm';
import { createElement, cloneElement, render } from 'react';

export const html = htm.bind(createElement);
export { h: createElement, cloneElement, render };

.. then use it within your project:

import { html, render } from './htm-react.js';

const App = ({ children }) => children;
render(html`<${App}>Hello World<//>`, document.body);

from htm.

developit avatar developit commented on June 3, 2024

Closing since this seems like a duplicate of a bunch of Preact fragments related issues, but feel free to re-open if I'm missing something!

from htm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.