Let's build your first App in React.

React is the most popular JavaScript library to build websites nowadays.

If you have never tried it, I want to show you today how to create a very basic counter app that will teach you the fundamentals.

Let's begin! 🧵
First step!

Having Node.js installed. If you haven't, follow this link and download and install the recommended version.

nodejs.org/en/
Now we are gonna open a terminal (cmd, Bash, PowerShell...) in the folder that we want to create our project.

Write this command:

npx create-react-app my-first-app

npx will download CRA latest version and delete it when it's done. "my-first-app" is the name of our app.
This will take a while.

Open the created folder with your IDE of choice, I'm using VSCode. You can download it here: code.visualstudio.com

You should see something like this: Screenshot of a newly creat...
Open your terminal in the folder, you can do it in VSCode with Ctrl + `.

Write this command:

npm start

This will run the app in development mode.

You can see it in your browser at http://localhost:3000.
Let's start building something!

First, clean everything in your App.js file except the outer div.

Then, we'll create a folder inside our src folder called 'components'. Inside there, we'll create a file called "Counter.js".

You should have something like this: Image
Add this code inside Counter.js

const Counter = () => {
return (

);
};

export default Counter;
We need to keep track of our counter and a way to update it.

So we are gonna use the useState React Hook.

useState returns an array, so we destructure it, first item is the variable and the second is a function to change the value to whatever we pass as argument. import { useState } from 'r...
The value we pass to useState is the initial value. 0 in this case.

In React, we never modify state directly, always through the setter.

So we can call our variable from inside our component.

Inside our div, let's call count. import { useState } from 'r...
But we can't see anything on the browser!

We need to import our Counter component on our App.js file.

So let's do it now.

Your App.js should look like this and you should now see a single lonely "0" on your localhost. Image
Now on Counter.js, let's add two buttons, one to increment and one to decrement.

Each button will have an "onClick" attribute, this is equivalent to a click event listener in JavaScript. Image
We are gonna pass our function to the onClick attribute and add our new value as the argument.

The reason we need to call an anonymous function is that if we don't, the function gets called immediately when the page renders, and we only want it to be called on click. import { useState } from 'r...
Now try it in your browser! It should update when you click the buttons!

Now try having 3 Counters on your App.js file.

Do you see what happens? Each component has its own state. You learned reusable components! import './App.css';  import...
And that's it! You created your first React App.

You learned how to use create-react-app, about reusable components, state, rendering, and the useState hook.
I hope this small thread helps you and motivates you to keep learning about React.

If you like this type of content follow me! And please suggest to me what you would like to see next.

If you have any problem following this tutorial, feel free to DM me and I'll help you.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Nacho Iacovino ⚡

Nacho Iacovino ⚡ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @nachoiacovino

15 Feb
JavaScript fundamentals before learning React.

Some people believe that you need to be a JavaScript master before starting learning React, and that couldn't be further from the truth.

You should know some fundamentals, and I want to tell you exactly which ones.

Let's begin 🧵 JavaScript fundamentals bef...
let and const.

This is the ES6 way of assigning variables, it replaces "var".

"let" is for variables that are gonna be reassigned in the future.

"const" is for variables that are not gonna change, you define them and you use them, but they have a "constant" value.
imports and exports.

A big part of React is reusability. You create a component (like a button), export it, and then import it on your other components without having to write it out again. /* components/Button.js */ ...
Read 11 tweets
23 Dec 20
Let's talk about Git.⚡

Git is the only thing that's it's common in our field. 99% of developers use it.

So you should learn it. At least the basics. And use it. On all your projects.

Let's start the thread.🧵

#100DaysOfCode #git #github #javascript #CodeNewbie #js What is Git? Let's learn th...
⭐ What is Git?

Git is a version-control system for tracking changes.

This allows you to save different versions of your projects and come back to them when necessary.

This also allows you to work in a team in a much better, organized way. ⬇
⭐ First steps.

On your project, your first command should be:

git init

This will initialize a new repository, this repository is a hidden folder called .git that tracks the changes made to files in your project and builds a history over time. ⬇
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!