Stream Props to Multiple Children with React's Children.map and RxJS

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet

Mapping the props to multiple children involves the use of React’s Children.map so that you can map over each child Component provided to the Parent Component stream. Inside the mapping function, you use the cloneElement and pass the props manually as you would with a single child component.

Instructor: [00:00] When you declare a second counter and hit save, you'll see everything breaks, because the way we currently have it set up, there's a contract of only one element. To support multiple elements, we come into here and say, "Give me the children, static helper as well," and then we can use that.

[00:17] We'll return children.map, and loop through all the props.children. Then use a function which takes the child, and then clones each of those childs, or children, in the same way we did before. Now, you see we have multiple counters here and multiple counters down here.

[00:40] As long as this contract holds between a component that takes the value on ink and on deck. Then we can create whatever component we want. We'll call this weird counter, and this can be a span, this can be a span, this can be an h1.

[00:57] Then we'll drop a weird counter in here, weird counter. Now, we have a counter component that looks completely different, but it takes the same props. It works just the same as the other.