Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris
There are many ways to create a component but let's have a look at how you can create a so called class based component.
We will do the following:
React.Component
and define the method render()
We use JSX to define it and assign it to a variable. Thereafter we are free to use it in our markup. To create our component we need the following:
Let's begin by creating a Jedi component:
class Jedi extends React.Component {
render() {
return (
<div>I am a Jedi Component</div>
);
}
}
Above we are defining the class Jedi
and have it inherit from React.Component
. Thereafter we define the method render()
that defines what our component will output. We return a JSX statement as output.
Now that we have our component we can easily it as we would any HTML element like below <Jedi />
:
<div>
<Jedi />
</div>
Ok, great I know how to create a Component but how do I actually create a React app?
That's a fair comment.
To create a React app we will opt to use the script
version of creating a React project. This involves creating thee following files:
index.html
<!-- index.html ->
<html>
<body>
<!-- This is where our app will live -->
<div id="app"></div>
<!-- These are script tags we need for React, JSX and ES2015 features -->
<script src="https://fb.me/react-15.0.0.js"></script>
<script src="https://fb.me/react-dom-15.0.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
<script type="text/babel" src="app.js"></script>
</body>
</html>
Above we create the mount point <div id="app"></div>
, this is where our app will be rendered.
app.js
app.js
looks like the following:
class Jedi extends React.Component {
render() {
return (
<div>I am a Jedi Component</div>
);
}
}
class Application extends React.Component {
render() {
return (
<div>
<Jedi />
</div>
);
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
The above has three parts:
Jedi
component.Application
and within its render()
method we place the Jedi
componentReactDOM.render()
with the Application
component and as second argument we tell it where to find the mount point. In our case this is the div
with id app