Rendering props.children without a wrapper
See original GitHub issueFirst of all, great job on Preact !
I noticed the following behavior being inconsistent with react unless there’s a preact way of doing it.
It would be nice if we didn’t need to wrap props.children with extra div’s
Example:
import { h, render, Component } from 'preact';
class TestNotOK extends Component {
render() {
return <h1 style="position:absolute; top:0; left:0">Doh! Test No worky!</h1>
}
}
class TestOK extends Component {
render() {
return <h1 style="position:absolute; top:20px; left:0">Doh! Test OK now!</h1>
}
}
class ProviderNotOK extends Component {
render() {
return this.props.children
}
}
class ProviderOK extends Component {
render() {
return <div>{this.props.children}</div>
}
}
// This does not work
render(<ProviderNotOK>
<TestNotOK/>
</ProviderNotOK>, document.body);
// This works
render(<ProviderOK>
<TestOK/>
</ProviderOK>, document.body);
Issue Analytics
- State:
- Created 8 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
In react can I render a component's children without a wrapper?
I've used the following code to only use a wrapper <span> on text children: render() { if (!this.props.children) { return null; } else...
Read more >Rendering props.children without a wrapper · Issue #45 - GitHub
Just return props.children[0] , no wrapper should be required. In preact, children is always an Array. This avoids needing to build an API ......
Read more >A Complete Guide To props.children In React - codeburst
Essentially, props.children is a special prop, automatically passed to every component, that can be used to render the content included between the opening ......
Read more >The mystery of React Element, children, parents and re-renders
Looking into what is React Element, exploring various children vs parents relationship in React, and how they affect re-renders.
Read more >React Children And Iteration Methods - Smashing Magazine
But in some cases, we want to inspect the children prop to maybe wrap each child in another element/component or to reorder or...
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
I don’t personally have a use for it, but I’m assuming it’s a feature Preact will need to support. It doesn’t fit well into the current model, but that’s something I’m already changing.
Hi @nightwolfz,
Just return
props.children[0]
, no wrapper should be required. In preact,children
is always an Array. This avoids needing to build an API for managing an abstract “maybe array” type.preact-compat
does include theReact.Children
for abstracting this if you need them to look identical.Here’s your code with the added
[0]
working: https://jsfiddle.net/developit/jqd96rhv/Cheers!