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.

Rendering props.children without a wrapper

See original GitHub issue

First 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:closed
  • Created 8 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
developitcommented, Mar 21, 2017

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.

6reactions
developitcommented, Feb 10, 2016

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 the React.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!

Read more comments on GitHub >

github_iconTop 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 >

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