Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 832 of the free egghead.io lessons, plus get React content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Access Nested Data with Reacts props.children

1:33 React lesson by

When you're building your React components, you'll probably want to access child properties of the markup. this.props.children accesses the innerHTML or nested components of another component.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

When you're building your React components, you'll probably want to access child properties of the markup.

Avatar
Ryan

HI I was having trouble using the React.renderComponent( ) method from the example, and it looks like the React team has changed the method that you should use in this case from React.renderComponent() to React.render()......https://medium.com/@_jh3y/adopting-change-with-react-v0-12-1d9625e27535

Avatar
Ryan

Hey how did you use the emmet like syntax when typing the span.glyphicon... and have webstorm know to use className instead of class (JSX vs HTML) ?

Avatar
Ryan

Hey how did you use the emmet like syntax when typing the span.glyphicon... and have webstorm know to use className instead of class (JSX vs HTML) ?

Avatar
Kevin

How do I setup bootstrap in my app? The video didn't show this...

Avatar
Joseph

Just add <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> to your index.html.

In reply to Kevin
Avatar
Somsubhra

Really Nice Video..Have been enjoying the series

Also found that the Button could be created in a functional(presentational) way

const Button = (props) => <button>{props.children}</button>
In reply to Joseph
Avatar
Olga

I'm still having trouble understanding this.props.children. When adding {this.props.children} to the child component, why doesn't it render a second button with the parent's information ?So in total there would be two buttons. I know my logic is wrong, but can someone tell me why this isn't the case ?

Avatar
Kirk Sefchik

I'm still having trouble understanding this.props.children. When adding {this.props.children} to the child component, why doesn't it render a second button with the parent's information ?So in total there would be two buttons. I know my logic is wrong, but can someone tell me why this isn't the case ?

I think I understand where you're coming from. The nomenclature here is very confusing -- it tripped me up as well. this in the Button class is indeed referring to Button (not <button> directly, but by extension). Whenever you pass something into the inner HTML of a component, it's populated in [child].props.children. If you install React Dev Tools for your browser (or console.dir(this) inside Button.render() { ... }...), you can see that Button.children is an array composed of 3 "children" passed to it by the App component: "I ", <Heart/> and " React". I know, silly right? They need to come up with a better name for this -- I don't know why they couldn't just call it [child].props.innerHTML. Regardless, I think that answers your question. :D

In reply to Olga

In order to access nested values or components in a component, we can use props.children. To illustrate this, I'm going to have my App component return a new component called <Button>, and inside of the opening and closing tags for that component, I'm going to type the word React.

App.js

import React from 'react';
class App extends React.Component {
  render(){
    return <Button>React</Button>
  }
}

export default App

That's the value we're trying to get at, React. We're going to create our stateless function component called Button, it's going to take in props and simply going to return an actual HTML button. To get at that value of react, which is nested in between our opening and closing tags, we're going to use props.children.

const Button = (props) => <button>{props.children}</button>

We're going to save that and there in the browser, we can see that we've created an HTML button, and we've allowed that in our HTML, or text content of react to pass through.

I also said we could access nested components, this time around, I'm going to create a class component, for no other reason other than to illustrate it a little bit differently.

I'm going to call this one my Heart component. It's going to return a <span>, and right here inside of that <span>, I'm going to drop the HTML entity for a heart symbol.

class Heart extends React.Component {
  render(){
    return <span>&hearts;</span>
  }
}

We're going to jump up here, back to our App component, and inside of the opening and closing <Button> tags, I'm going to say I, we're going to drop in our nested component, which is going to be our Heart component.

render(){
  return <Button>I <Heart /> React</Button>
}

We're going to save that, and we can see that using props.children allowed both the text values, as well as the nested component to flow on through into our HTML button.

Button



HEY, QUICK QUESTION!
Joel's Head
Why are we asking?