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.

Component that accepts children

See original GitHub issue

I am trying to write a wrapper component that puts a panel around whatever children are passed.

In React, I can usually do something like

var Frame = React.createComponent({
  render: function() {
    return <div>{ props.children }</div>
  }
});

and then use it like

<Frame>some content</Frame>

I assume that in Scalajs-React I can explicitly pass some props that contains Seq[ReactComponents] as a property, but I was wondering if the case of passing children, which has special treatment in React, also gets a special treatment here

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
andreaferretticommented, Feb 20, 2015

Thank you both, it works perfectly!

For future readers: what works is passing the children after the props when instantiating the component, and using a form of render with two parameters, like this:

val Panel = ReactComponentB[Props]
  .render((props, children) =>
   div(className := "hello", children)
  )
  .build

...
div(Panel(someProps, div("world")))
0reactions
marcadellacommented, Mar 11, 2022

Just for the record: to make a childless component accept children,

  • Replace .renderBackend[Backend] with .renderBackendWithChildren[Backend]
  • Replace def apply(p: Props) = component(p) with def apply(p: Props)(child: ChildArg*) = component(p)(child:_*)
  • Replace def render(p: Props, s: State) with def render(p: Props, children: PropsChildren, s: State)
  • Use the children in the render method (for example using vdomNodeFromPropsChildren(children))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Composition vs Inheritance - React
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
Read more >
A quick intro to React's props.children | by Jason Arnold
The React docs say that you can use props.children on components that represent 'generic boxes' and that 'don't know their children ahead of ......
Read more >
React Children And Iteration Methods - Smashing Magazine
In this article, we'll discuss and learn about the use case of iterating over React `children` and the ways to do it. In...
Read more >
Using Children in React - We Learn Code
You can use props.children in React in order to access and utilize what you put inside the open and closing tags when you...
Read more >
Component Children | Build with React JS
Children allow you to pass components as data to other components, just like any other prop you use. The special thing about children...
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