Sitemap
Peter Meister

Peter Meister

Recent graduate from The Flatiron School and aspiring Web App Developer

Getting Started with React

4 min readOct 4, 2018

--

A Beginners Guide To Setting-Up a Basic React App

Get excited, because “there must be a better way” isn’t referring to some ethereal future notion of understanding…it’s referring to React.

Get Peter Meister’s stories in your inbox

Join Medium for free to get updates from this writer.

If “excited” isn’t the right word, then I’ll let you stick to a react-ion your most comfortable with

Getting Started

Let’s start with going into our console.

create-react-app blog-example

This will install all the appropriate packages and scripts that a basic React app will need.

Once your console is done downloading everything it needs, go into your new project directory and start your app to make sure it is set-up properly.

//blog-example
npm start

App.js

Now that your app is connected, you can start molding it into what you want. As you can see, your React app comes with a few JavaScript files. For the sake of this example, we will focus on what’s going on in src > App.js:

Press enter or click to view image in full size

As you can see, this React app has some default code rendered in its App.js file. There are a few things going on here that are key setting up and connecting any future files down the road. Let’s point them out and explain what their functions are.

Import/Export

At the top of the App.js file, we have a few lines of code that start with ‘import’. Remember when we did ‘create-react-app’? During that download, we were getting all the related packages and scripts associated with a React app. These imports are grabbing everything App.js will be using from outside sources (i.e. ‘react’, ‘./WelcomePage’).

At the bottom of the App.js file, we have a default ‘export’ function that simply exports this App.js file as ‘App’. If another file wants access to App.js, it would import App from ‘./App.js’

App.js — Class Component

Below is a simple set-up to our App.js file. What we are doing is creating a class App, setting its ‘state’, an object that determines how that component renders & behaves, to have the name: “Billy”, and telling it to render the <WelcomePage/> Component in a <div>.

As you can see, we are passing the ‘state’ object to our <WelcomePage /> Component by assigning ‘this.state.name’ to name. Now our <WelcomePage /> Component knows what name is.

WelcomePage.js — Functional Component

Below is a simple set-up to our WelcomPage.js file. What we are doing is creating a function sayHi that takes in ‘props’ as its argument, returns an <h1> tag that says ‘Hi, {props.name}’, and exports itself.

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

The Result

Now, when we go to our http://localhost:3002, we can see the results of our work:

Summary

  • We created a simple React app
  • Set up our App.js file to import the Components we need from their locations
  • Set-up our ‘state’ object to hold the name of the person we want to say “Hi” to
  • Passed our ‘state’ object to our WelcomePage Component
  • Retrieved our ‘state’ object information from App.js to WelcomPage.js
  • Called that ‘state’ information ‘props’, and told our WelcomePage Component to render the name via ‘props.name’

--

--

Peter Meister
Peter Meister

Written by Peter Meister

Recent graduate from The Flatiron School and aspiring Web App Developer