Learning React.js
Tags: javascript, beginners, programming, react
Published: 2021-12-09 20:47:57

React.js is a Javascript library that allows building user interfaces. There are three main concepts that you need to know before starting your journey with React. So, here you will learn about these concepts and how you can use them in your daily

## Components

A component is a part of code where you can turn in independent parts and encapsulate them inside a function, and reuse them between other parts of your code. It's possible to write components through functions and classes.
This example will be written through functions:

- First, you will select the file App.js and create a const called as you want, here I use the example of forecast and added strings.
    
![Adding constant](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f76akligv9w7ruek5ptt.png)
- Now it's time to create our component. So, let's make that.

![Creating Component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb2hfmsr4bq0lqmursrb.png)

1) We need to add a div or <></> inside of return.

2) You should select your const and add map function, because we want to iterate every item inside our const.

3) You should create the other variable inside map just to start the function, you can call as index, item, or anything like that, I chose climate because it's aligned with the forecast.

4) Now, you should add ⇒ to return something, so I put an HTML tag and passed climate inside of keys as my result.

### Output:

![Result of Component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ad0us7ed531sw08c4mop.png)

- As you can see, we are using h1 three times because we have three strings inside of forecast constant. Now, we can conclude that we are reusing the same component three times, in this case, I chose an "h1" but you can use another HTML tag.

## State

The state is used to save the data of the component, it's a simple variable that defines an initial and final state, where the final state is considered a mutable state, where you can change the initial state of some component. The state will change the VirtualDOM and when the state is updated, the component renders the new state, the mutable state.

- Here, I will use a Hook, but you can make it with a constructor following the docs.

1) You need to import the Hook called useState in your App.js file.

2) We will change a little bit the way that we declare our constant. Now, you should use the Hook useState and pass your input params. Here, I'm passing forecast as the initial state, and setForecast as our final state.
![Adding state](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqcozkpfywn44byokhib.png)
I added more properties inside of forecast because we want to change them using setForecast.

3) With our params created, it's time to handle the setForecast, so let's create a function to change our initial state.

![handle the function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6445yo8i49mrmg27xd8m.png)

4) We need to organize our code, it's important to create a folder where we will save our components. Inside of src I create a folder called components and added a Weather.js file.

![Creating component folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkxfzt7i82it5mno19vg.png)

5) After that, we want to show our output. Inside of Weather, you should create a function that receives a forecast as a parameter and add your return with HTML tags.

(Don't forget to export your component)

![weather component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypg0nrr255be3iqyu5zi.png)
As you can see, I'm passing forecast inside of "h1" and "p", here we are calling the forecasts constant and accessing the properties inside of an array.

6) Now, you need to come back to App.js and create a button to execute the handleChangeForecast function. 

We still using map function to iterate every item and you should Import your component inside of App.js to use in the return statement.

![Function to show the output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb5zt20s9ai5m8w23tjx.png)

## Initial State

![Inital State](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2a7hy4xvyba7xbok9ng.png)

## Final State

![Final State](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trd5ciapymh2kfca7b71.png)

## Props

Props it's the abbreviation of properties, they are passed between the components and return elements that will be shown through the DOM. A Characteristics of props is immutability, you can't change them.

![Props](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xg24q4g8o2we32fl4hp.png)
 

Here, we are passing a key with forecast ID and forecast property, this props can't change, you can't change the ID, but we are passing them to identify throw the iteration of map.

Hopefully, the tutorial above helped you to learn more about React concepts. If you have any doubt, feel free to leave comments about them.

If you learned something from this article, please hit the like button.

- If you want to talk with me, please send me a message on [Twitter](https://twitter.com/matheus__tanaka).
- [Source Code](https://github.com/matheustanaka/react).
- [Linkedin] (https://www.linkedin.com/in/matheus-tanaka-42a833186/)