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.

Preact Children Props Array is always empty

See original GitHub issue

Preact doesn’t count children returned from a component’s render function as children for example

render(props, state) { return(<div><ComponentOne/><ComponentTwo/></div>); }

will always have props.children = []

This is inconsistent behavior from React where the children props absolutely are counted. Also before you close this issue as a non-bug, please give guidance on how to properly get props.children to show the correct information

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, Aug 5, 2018

What is the correct way to access children from a component?

class Foo extends Component {
  render(props) {
    props.children  // [<span />, "hello"]
    return h('div', { id: 'foo' }, props.children);
  }
}

render(<Foo><span />hello</Foo>);

props.children is always an Array containing the JSX elements and text values passed as children to a given component. It’s normalized: h('div', { children: 'one' }).children === ['one']

We may consider moving towards React’s behavior (no normalization whatsoever) in future versions, but it would be a fairly major breaking change and would require shipping the entire Children interface (likely 500b on its own).


Note: I found both your examples confusing - you’re returning children from a render function, not passing children to a render function:

<Foo><span />hello</Foo>
// roughly translates to:  new Foo().render({ children: [<span />, 'hello'] })

class Foo {
  render(props) {
    // props here are the props passed to <Foo>, which means
    // props.children here has nothing to do with the return value of this function.

    return <OtherComponent>hello</OtherComponent>
                         // ^^^ OtherComponent.render() will receive { children: ["hello"] }
1reaction
pl12133commented, Aug 1, 2018

Please don’t open duplicates: https://github.com/developit/preact/issues/1173

If you create a running code sample to demonstrate the differences between React and Preact it might be easier to understand your question.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preact Children Props Array is always empty #1174 - GitHub
This is inconsistent behavior from React where the children props absolutely are counted. Also before you close this issue as a non-bug, please ......
Read more >
React: props is always an empty array when passed to child
When I log the safedEvents array in the parent class it is not empty. The safedEvents array is initialized via useState(). Here is...
Read more >
Jason Miller on Twitter: " POLL: Preact currently represents "no ...
POLL: Preact currently represents "no children" as an empty props.children Array. Do you think this should be changed (back) to null?
Read more >
API Reference | Preact
When there are multiple children, props. children is always an Array. The toChildArray helper provides a way to consistently handle all cases.
Read more >
Array Passed in Props Showing Up as Empty - Stack Overflow
The actually reason you face this strange behavior is because your code App is wrapped with StrictMode that renders your component twice.
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