--- id: 5a24c314108439a4d403616e title: Access Props Using this.props challengeType: 6 forumTopicId: 301375 dashedName: access-props-using-this-props --- # --description-- The last several challenges covered the basic ways to pass props to child components. But what if the child component that you're passing a prop to is an ES6 class component, rather than a stateless functional component? The ES6 class component uses a slightly different convention to access props. Anytime you refer to a class component within itself, you use the `this` keyword. To access props within a class component, you preface the code that you use to access it with `this`. For example, if an ES6 class component has a prop called `data`, you write `{this.props.data}` in JSX. # --instructions-- Render an instance of the `Welcome` component in the parent component `App`. Here, give `Welcome` a prop of `name` and assign it a value of a string. Within the child, `Welcome`, access the `name` prop within the `strong` tags. # --hints-- The `App` component should return a single `div` element. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(App)); return mockedComponent.children().type() === 'div'; })() ); ``` The child of `App` should be the `Welcome` component. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(App)); return ( mockedComponent.children().childAt(0).name() === 'Welcome' ); })() ); ``` The `Welcome` component should have a prop called `name`. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(App)); return mockedComponent.find('Welcome').props().name; })() ); ``` The `Welcome` component should display the string you pass as the `name` prop within `strong` tags. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(App)); return ( mockedComponent.find('strong').text() === mockedComponent.find('Welcome').props().name ); })() ); ``` # --seed-- ## --after-user-code-- ```jsx ReactDOM.render(, document.getElementById('root')) ``` ## --seed-contents-- ```jsx class App extends React.Component { constructor(props) { super(props); } render() { return (
{ /* Change code below this line */ } { /* Change code above this line */ }
); } }; class Welcome extends React.Component { constructor(props) { super(props); } render() { return (
{ /* Change code below this line */ }

Hello, !

{ /* Change code above this line */ }
); } }; ``` # --solutions-- ```jsx class Welcome extends React.Component { constructor(props) { super(props); } render() { return (
{ /* Change code below this line */ }

Hello, {this.props.name}!

{ /* Change code above this line */ }
); } }; class App extends React.Component { constructor(props) { super(props); } render() { return (
{ /* Change code below this line */ } { /* Change code above this line */ }
); } }; ```