2018-10-10 18:03:03 -04:00
---
id: 5a24c314108439a4d4036168
2021-02-06 04:42:36 +00:00
title: Write a React Component from Scratch
2018-10-10 18:03:03 -04:00
challengeType: 6
2020-09-18 00:13:42 +08:00
forumTopicId: 301424
2021-01-13 03:31:00 +01:00
dashedName: write-a-react-component-from-scratch
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Now that you've learned the basics of JSX and React components, it's time to write a component on your own. React components are the core building blocks of React applications so it's important to become very familiar with writing them. Remember, a typical React component is an ES6 `class` which extends `React.Component` . It has a render method that returns HTML (from JSX) or `null` . This is the basic form of a React component. Once you understand this well, you will be prepared to start building more complex React projects.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Define a class `MyComponent` that extends `React.Component` . Its render method should return a `div` that contains an `h1` tag with the text: `My First React Component!` in it. Use this text exactly, the case and punctuation matter. Make sure to call the constructor for your component, too.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Render this component to the DOM using `ReactDOM.render()` . There is a `div` with `id='challenge-node'` available for you to use.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
There should be a React component called `MyComponent` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
assert(
2021-02-06 04:42:36 +00:00
__helpers
.removeWhiteSpace(getUserInput('index'))
2020-12-16 00:37:30 -07:00
.includes('classMyComponentextendsReact.Component{')
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`MyComponent` should contain an `h1` tag with text `My First React Component!` Case and punctuation matter.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
(function () {
const mockedComponent = Enzyme.mount(React.createElement(MyComponent));
return mockedComponent.find('h1').text() === 'My First React Component!';
})()
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`MyComponent` should render to the DOM.
2020-09-18 00:13:42 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(document.getElementById('challenge-node').childNodes.length === 1);
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-02-06 04:42:36 +00:00
`MyComponent` should have a constructor calling `super` with `props` .
2021-01-13 00:22:19 +08:00
```js
assert(
MyComponent.toString().includes('MyComponent(props)') & &
MyComponent.toString().includes('_super.call(this, props)')
);
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```jsx
// Change code below this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```jsx
// Change code below this line
class MyComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
< div >
< h1 > My First React Component!< / h1 >
< / div >
);
}
};
ReactDOM.render(< MyComponent / > , document.getElementById('challenge-node'));
```