Build a website tutorial pdf




















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. It should prompt that you have successfully registered. Try going to phpmyadmin and see your users table:.

Now for the login page. Let's create a new file called checklogin. The reason is going back to our login. If you will notice on the register.

Click here for the complete checklogin. Here's the explanation of the code. Some are explained earlier so there's no need to reiterate :. Now try to test your input with a wrong username and password. It should return the desired prompt. After testing, try inputting the correct values. It should lead you to home. Now that were authenticated, let now create our home page home.

Now that we have our home, let's try creating our logout. What we would do is that if the user is logged-out, the user shouldn't access home. So here's the simple syntax to logout. The syntax is simple. Now try refreshing home.

Now try clicking on the back button of your browser and see what happens:. As you can see, it doesn't direct you to home. The same case should also happen. Since were logged-out, even a manual input of the address doesn't access an authorized page. What we have done is a simple security mechanism wherein we redirect back unauthorized users into a public page.

In our next step, let's create the adding of items to the list. Take note that this ain't our official add. Now go back to your home. As you can see, we have our current time, date, and your input. Here's the explanation to the code:.

Now let's modify our add. Click here for the complete add. Now try entering some data and click " Add to list ". In my case, I'll just use finish again. Let's go to our phpmyadmin and let's see if the data has been added. Here's the result of my case:. Now that we have seen that the data has been successfully added, let's now display the data in our home page. Let's modify our home. The explanation to the added code is quite simple. It just basically displays the data coming from the while loop.

It has been explained earlier in our tutorial so I believe that by this point, you should have understood the process of getting the data in the query. Going back to the browser, try refreshing your home. It should now display that data. Next is to update edit and delete information. If you will notice we have edit and delete links displayed on the column.

I'll add another data to the list named " tuna " to have another example and this time, it's privacy to no :.

Let's now try editing our data and to do that we will use a new functionality called " GET ". To start of, let's modify our home. If you have noticed, we only added URL parameter for the edit and delete links namely id. We will be using this later to handle thee data. The reason why we use id is because it's a unique identifier.

It is possible for the person to have enter the same data so it's not recommended to use the details as a mean for manipulation later on. Try putting your cursor into the edit link and you will see the value of the id on the lower left:. Now that we have that, let's try creating our edit. Let's try modifying the URL parameter by removing?

Now try putting a value that is greater than the id number, in our case, let's try 5 and it should result like this:. Now that we secured our URL parameters, let's now place the edit syntax. Let's go back to edit. Click here for the complete edit. Now try refreshing and go back to the edit page.

Let's try a different data. Hacktoberfest Contribute to Open Source. Download the Complete eBook! About the authors.

Erin Glass. Still looking for an answer? Ask a question Search for more help. Comments Follow-Up Questions. Before you can do that To complete this action, sign in to your Community account or create a new one.



0コメント

  • 1000 / 1000