preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.72k stars 91 forks source link

This JSX tag's 'children' prop expects a single child of type 'ReactNode', but multiple children were provided #323

Closed childrentime closed 1 year ago

childrentime commented 1 year ago
import { signal } from "@preact/signals-react";

const count = signal(0);
function App(){

return  <button
          onClick={() => {
            count.value = count.value + 1;
          }}
        >
          count is {count}
        </button>
}

just a simple demo code will throw typescript error, maybe we should export a new jsx type to satisfy jsxImportSource option?

rschristian commented 1 year ago

This is due to React 18's newer, more strict children types. To fix this, you can simply wrap the content in a fragment:

import { signal } from "@preact/signals-react";

const count = signal(0);
function App() {
    return (
        <button onClick={() => count.value = count.value + 1}>
          <>count is {count}</>
        </button>
    )
}

Patching React's types is not a good idea, no, nor is it specific to this library. Any library which uses objects within JSX will likely have this issue.

christowiz commented 1 year ago

@rschristian It would be nice if this was add to the documentation and code examples so other people wouldn't have to search Github issues to figure out the problem.

rschristian commented 1 year ago

If you have a suggestion for how to improve the docs, PRs are always very welcome.

christowiz commented 1 year ago

@rschristian Docs PR: https://github.com/preactjs/signals/pull/385