React js app from scratch download
But the structure of React makes it useful for more than just creating websites. React Native uses React to build mobile applications. React encourages engineers to write code using a Functional Programming approach. Engineers create components, which are normal Javascript functions. These functions return information to tell React what content should be displayed on the screen.
The real power of React comes from the ability to nest or compose these functions inside of one another.
This nesting ability encourages code reuse and allows an engineer to write a component a single time, but in many different places. Frameworks provide an opinionated approach to building an entire application.
Libraries, on the other hand, assist in building a single aspect of an application. With this in mind, React is a library. It assists engineers in presenting HTML in the browser.
React has no opinions on how data is fetched, how styling is applied, or how the app is deployed or built. Yes, React is worth learning. There are a couple of reasons. The first one is that React is in high demand in the software development job market and has been for a few years. If you learn to code in React and can do it well, you will increase your chances of finding a job.
This alone is another reason it is worth learning. React has also been around a few years and has stood the test of time. A large portion of websites, both small and large, use React as a frontend framework. The compressed, production version of React with optional add-ons. We recommend using React from npm with a bundler like browserify or webpack. You can use the react and react-dom packages. After installing it using npm install --save react react-dom , you can use:.
Now, instead of rendering a simple JSX element, we are rendering a React component. This is necessary so that its instance e. Also, take note of the component file path as used in the index. Make sure you always specify the relative path of that file from the current directory. In our case, ". Meaning the TodoContainer file is located in the components folder inside the current directory. The file extension defaults to. React provides for us the StrictMode to activate checks and logs a warning message at runtime.
This enables checks and warning not only for the component but also its descendants. If you want to activate check for a particular component, you should wrap that component instead of the root component. It may be a child component receiving data from its parent or maybe the user directly input data to the component. Understanding how the data flows is very crucial to building React component.
That brings us to the concept of state and props. It can be thought of as the attributes in the HTML element. For instance, the attributes — type , checked — in the input tag below are props. When this happens, the data that is received in the child component becomes read-only and cannot be changed by the child component.
This is because the data is owned by the parent component and can only be changed by the same parent component. Unlike the props, the state data is local and specific to the component that owns it.
It is not accessible to any other components unless the owner component chooses to pass it down as props to its child component s. Maybe it was inputted or comes from the props. Also, if two or more child components need to communicate with each other. Now, once the component receives this input data, we need to pass it to a central location where we can manage it and display in the browser view. For instance, the TodosList component will be accessing the data and display its todos items.
Also, the TodoItem component which holds the checkbox and delete button will be accessing the data to update the checkbox, update edited items and also remove items from the state. Now, for every child component that will be accessing the data, you will need to declare the shared state in their closest common parent. For this reason, the shared state data will live in the TodoContainer component, which is their closest common parent. This parent component can then pass the state back to the children by using props.
Though, instead of declaring a shared state in the parent component as mentioned above, an alternative is to use the Context API to manage the state data. As a beginner, you should explore all options. In this React tutorial series, we will start with the simplest of them.
Once you have the basic knowledge, you can then learn to use the Context API for your state management. To add a state in a class component, we simply create a state object with key-value pair.
The value can be of any data type. In the code below, the value is an array. If you look at our design critically, we will be updating the to-dos checkbox.
This implies that we need to make provision for that. So a typical to-dos item will look like this:. Now, instead of an empty array, we will have an array of objects. So add the following code just above the render method in the TodoContainer. Still in the file, update the render method so it looks like this:. After we defined the todos data in the state object, we accessed it in the render method using this.
In addition to the earlier explanation, the render method is one of the lifecycle methods more on this later that React call during the Render phase. This phase is when React decides what changes need to be made to the DOM. Since the value of the todos is an array of objects as declared in the state , we looped through this array and output each of the todos item i.
In React, we make use of the map method which is a higher-order function to do this iteration. We will take care of that in a moment. For now, I want you to compare the frontend result and the app diagram.
You will realize that another component called TodosList has the responsibility to handle the todos list. This is where we will apply the knowledge of props earlier explained. What we want to do is to pass the state data from the TodoContainer down to the TodosList child component. Recall that we can pass data down the tree as props. First, go inside the TodosList. At this point, you can render anything. We will update it soon. After that, open the TodoContainer. So, add this at the top of the TodoContainer.
At this point, you now have the state data in the todos prop. Thanks to this line:. Now, we can access this data through props in the TodosList component.
Save your file. You should have the todos title rendered on the screen just like before. Notice how we accessed the state data from within the child component, TodosList , using this. Always remember, with props, we can access state data at different levels of the component hierarchy. This is called prop drilling. And it has to do with manually getting data from component A down to component B through the props.
Where component A is the parent of B. Then, we accessed it through this. Whenever you map through something, a list is created. React want each child in the list to have a unique key prop. This helps React to identify which items have changed, added or removed.
To add this unique key prop, we will take advantage of the id we provided in the TodoContainer state. We can access these id s the same way we accessed the title. We did something like this earlier. Open the TodoItem. For the meantime, you can render anything.
Next, import the component in the TodosList. Note: Since we are mapping through the todos, the key prop must be present. At this point, each of the state data is present in the todo prop.
You can now access these data through props in the TodoItem component. You should have the frontend displayed as expected.
In the TodoItem component, take note of how we accessed the title using this. If you want to inspect and debug your application, check your components tree or see how React works in real-time, you will need this tool.
It is available as a browser extension for Chrome and Firefox. Head over to the extension page for your browser of choice Chrome here and Firefox here and install it. To view it, open the browser devtools by right-clicking anywhere on your web page viewport and select Inspect or Inspect Element depending on your browser.
Then, on the browser inspection window, select the Components tab to see the view of your application hierarchy. Please note that you will not be able to see the Components tab if your webpage is not using React at the moment. You can navigate through different component in the tree and view the state and props data.
Just play around with it for now. Though, later in the series, we will use the functional component to manage our app functionality i.
But now, I want to quickly show you how to easily integrate this component type in your app. As you have guessed, for now, the component will not be managing any logic. If you take a look at the components we created, only one of them is holding the state data. That is the parent component, TodoContainer.