React.js Beginner's Tutorial

Source: Internet
Author: User

As you can guess from the title, the goal of this article is to give readers who have little programming experience. For example, people like me: Because so far, I have explored the programming world for 6 months. So, this will be a beginner's Village tutorial! you only need to have the understanding of HTML and CSS, as well as basic JavaScript (JS) knowledge can read this article.

Note: In the following example, we will use the new capabilities provided by ES6 to simplify the process of writing JS code. However, you can also use ES5 to write React completely.

Estimated reading time 9 minutes

What is React?

React is a JS library, created by Facebook and Instagram (https://facebook.github.io/react/). It allows us to create a single page application (applications) by dividing the application into dynamic, reusable components .

A React component is a JS class that inherits the Component provided by the React. A component represents and defines a piece of HTML code, and any behavior associated with that code, such as a click event. Components are like Lego bricks that can be used to form the complex applications needed. A component composed entirely of JS code that can be isolated and reused. The basic method is render (), which simply returns a piece of HTML code.

This syntax for defining React components is called JSX. This syntax was developed by the creators of React, and is used to simplify the intra-component interaction of the js-html code. Code written using this syntax must be compiled before it becomes the actual JS code.

Create a component (component)

In order to create our component and render it as a page of HTML, we first need to define a div with a unique ID in our HTML file. Next, we will write the code in the JSX file to connect the React component to the DIV that uses its ID, as shown in the following example. Doing so will instruct the browser to render the component on the page where the relevant DOM tag is located.

See the Pen Start by Makhenzi (@makhenzi) on Codepen.

The HTML tag attribute in JSX is almost the same as in normal HTML; the only difference is "class", which becomes "ClassName" in JSX. The class HTML syntax is closed with parentheses, while the block containing JS is closed with angle brackets. As you will see. Render () always returns a Div, where developers are free to introduce as many tags and elements as they deem appropriate.

Example: Extinction of Pirates

If we choose to use React to create this graph, we can visualize each date on the screen and display the corresponding temperature and number of pirates when those dates are clicked.

For this we need 2 components: the first to render the date, and to link each date to the given number of pirates and the temperature; the second one needs to receive the information corresponding to the click events on the date, such as the number of pirates and the temperature at that time, and then renders the selected elements based on the data.

The former is equivalent to the role of "Father", and contains multiple links to later "child" components, while the latter is closely dependent on their "father".

The React structure, known as the virtual DOM, allows us to change the contents of a component without having to refresh the entire page, but only to update the corresponding component. To do this, the component needs an internal method to hold the variable data and the changed HTML attributes assigned to the element. These properties are linked by themselves to the methods that we define within the component that are responsible for responding to changes.

Status (state) and attributes (props)

In our case, the independent variable data is made up of dates. These can be changed according to the corresponding pirate and temperature information based on the intra-DOM chain reaction set by the Click event. Therefore, we will save the information according to the corresponding date within each "DATA" object. We will also use the properties of React in the parent component to this.state={} save the variable data as a key-value pair in copy form.

Organizing the program in this way allows us to use the method provided by React to interact with the data in the form of "state" and make any changes to it.

Given that we want to use the key of the DATA object to render the date within the HTML, it is best to find a way to use the JS map() method (Array.prototype.map ()) on the key so that the returned HTML can be displayed directly render() . There is actually a way to do it! We just need to wrap the JS code in a double curly brace and place it in the DOM block that we want the code output to display to manage that component, and then it's OK.

In this particular case, we will define the callback in the method within the component map() , which will return a piece of render() HTML code within the same component.

See the Pen State1 by Makhenzi (@makhenzi) on Codepen.

In order to allocate the click events to each date, we will assign onClick attributes to them.

In this property, we call the component's method, which defines the state modifications and other changes we want to trigger after the OnClick event.

In our example, we define the function as handleClick() . In Handleclick (), we call the React method setState() , which allows us to change the state data in each click event. We just need to insert an object that contains the state key that we want to modify, and assign them new associated values within the parentheses of the latter.

In general, each time a date is clicked, the OnClick property of the selected Div invokes the HandClick() method, which invokes the SetState () method to modify the state of the component.

Each time the state changes, once the React occurs, the function of the component is automatically render() returned to look for content that needs to be updated based on the new state. Once that data is available, React automatically triggers a new one render() to update the HTML fragments that have changed.

(I'm sorry, in the next example, I inserted three lines using the Classnames code, a widget for CSS management based on state changes, and I did this just to give the preview a bit of color.) I'll also use it to populate the preview with some pirate variables in the final example. You can find links to classnames warehouses on GitHub, as well as a brief usage guide)

See the Pen State2 by Makhenzi (@makhenzi) on Codepen.

Thus, the state of our parent component has been set to create subcomponents based on the selected data (which will describe the number of pirates and the corresponding temperature).

We will create an instance of the subassembly in the JSX file, as we did with the parent component. In order to link subcomponents to their fathers, we only need to use the render() same syntax and an HTML tag in the latter's function to define the relationship. If we call it "child", it will <Child /> appear within the HTML block where we inserted it.

Our subcomponents must also pass data to their fathers based on the pirate and temperature associated with the currently selected data. To do this, we will use the attribute assigned to the child tag, whose name can be arbitrarily taken and whose information is visible only to the parent component.

As a result, subcomponents will be able to gain access to their own internal information by explicitly accessing the data that belongs to their parent component, using these "attribute-bridges", or attributes (props).

Therefore, each time the state of the parent component changes, the property content of its subcomponents is automatically updated. However, just as the child component's render() method displays the property content, it is also based on a one-way data line flow and updates based on any new information that is received.

It's done! Components interact with each other and render different data in the DOM based on our clicks, without needing a single page to refresh. Based on this, the complexity of interactions and the number of components can be increased as needed, enabling us to create complex and efficient applications.

If you're inspired by the potential of this library, take a look at the react.js example website where you'll find lots of interesting ideas to help you get started. (:

React.js Beginner's Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.