React 1 - Hello React
This is the beginning of a more detailed introduction to building React based applications. For those looking for a super fast introduction refer to my other blogpost. Todays goal is to get a basic component up and running, transpile the JSX and ES6 and finally the the result in our browser.
The base idea of React is simple enough. Describe how your application should look given a certain state. The beauty of this is how easily we can reason about it. No more thinking about how user input goes back into the system. We just describe how our site looks.
Just describe how your application looks, given a state.
Pre requireries
You will need Node installed on your system. I also strongly suggest to set up a package.json
file to keep organized and run scripts from. The rest of this article and series will assume that you do so.
You should also be familiar with ES6
Components
To organize things even more you can break your application down into so called components. They are a part of your application that might be used multiple times. Like a button or the way a users avatar is displayed. Lets take a look at a simple component.
|
|
Lets take a look at this line for line. At the top we import React which you always need to do in all your components.
|
|
Then we define the button which is a function returning a JSX component and taking two arguments named text
and click
. We will later be able to pass properties with these names to our component. This is an ES6 function syntax using an implicit return.
|
|
Explicitly returning would also be possible and looks like this:
Note that a component should always have a single outer wrapping element.
JSX
JSX is a HTML like syntax you can use to describe your component. You can write plain HTML with custom tags and use {}
to add things, like render the text of a variable.
|
|
Notice that to add a class to our component we have to use className
instead of class
. Here we use {}
syntax to render the text of the button and add a function to be called when the button is clicked.
Entry point
Each React application needs an entry point. This file will wrap it all together and tell your application what to render. So lets set up to render some “Hello React” on our button.
|
|
First we once again import React
and this time also render
which is a function render a React component into a given DOM element. Then we also import our Button
, by convention we capitalize components to distinguish between custom and native HTML tags in our JSX.
|
|
Further down we simply call render with our button and giving it some properties.
See the result
We are going to “compile” our application into a folder called build
. To actually see your application later create a build/index.html
file. Make sure it has an element with id app
and includes the script.
|
|
We will be using Webpack to bundle our application together. You will need to install Webpack. Let’s also install a local server with live reloading features so we don’t have to refresh the browser to see our changes. While we are at it we should also pull in React and some stuff Webpack needs, we will go over all the packages.
|
|
Package | Description |
---|---|
react | The basic React package |
react-dom | rendering React components inside a DOM element |
live-server | A local server with live reloading of changed files |
webpack | Bundler for our application |
babel-core | Babel does the actual transpiling |
babel-loader | Enables Babel use in webpack |
babel-preset-es2015 | Transpiling of ES6 |
babel-preset-react | Transpiling of JSX to React |
So finally we can set up two script in our package.json
and run them both to see the results.
|
|
Run them in separate consoles (use npm run webpack
) to have both going at the same time. Your browser will pop open and show you the resulting page. Any changes you make will trigger Webpack to bundle your application again and live-server to reload the page.
Wrap up
We now have a basic setup for React. Follow the next parts of this series when we look at composing components and hooking up Redux as a state manager.
⬅️ Go back