React technical principles and code development practice: combined use of React and Redux

Author: Zen and the Art of Computer Programming

1. Background Introduction

React is a JavaScript library for building user interfaces, created and open sourced by Facebook. Like other JavaScript frameworks, React’s biggest feature is that it is easy to use and can easily implement various functions. React is not only a front-end framework, it is also a full-stack solution, including back-end services, databases, and even Including unit testing, etc. Therefore, React also has strong practicality. However, due to the steep learning curve, it is often difficult for beginners to fully master all its features. Therefore, the author of this article starts from the underlying principles of React and analyzes the basic concepts, operating mechanisms, core algorithms and details of React in a simple and easy-to-understand manner. Through a large number of code examples, the author explains the application scenarios and actual operation steps of React in actual projects.

2. Core concepts and connections

What is React?

React (Reactive, reactive) is a JavaScript library for building user interfaces (UI). It is designed for declarative programming, which only describes how the application should look, but not how to update or render these elements.

What is Redux?

Redux is a JavaScript state container that provides predictable state management. It allows you to store all of your application’s state in a single repository instead of multiple separate stores. Redux provides a complete solution, including creating stores, defining actions and reducers, and asynchronous data stream processing based on Redux.

The relationship between React and Redux

React itself is just a library, while Redux is a state manager responsible for storing and modifying the application’s data. React can work with Redux, but they are still closely related. Generally speaking, when React is used as a UI framework, we will use Redux to manage the state of the application; if React is only used for the display of small components, or there is no too complex interaction logic, then React itself can also manage the state.

3. Detailed explanation of core algorithm principles, specific operation steps and mathematical model formulas

Use React to make a simple front-end page

Suppose we already have an HTML file and a JS file. We want to insert a button into HTML and execute a function when the button is clicked. As shown below:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>React App</title>
    <script src="//i2.wp.com/unpkg.com/react@17/umd/react.development.js"></script>
    <script src="//i2.wp.com/unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <script src="//i2.wp.com/cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.1/babel.min.js"></script>
  </head>
  <body>
    <button id="myBtn">Click me!</button>

    <div id="root"></div>

    <!-- index.js -->
    <script type="text/babel" src="./index.js"></script>
  </body>
</html>

The content of index.js is as follows:

class App extends React.Component {
  handleClick = () => {
    console.log('Clicked!');
  }

  render() {
    return (
      <div>
        <h1>Hello World</h1>
        <button onClick={this.handleClick}>Click me!</button>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

In this way, we have completed a most basic React application. However, this example is not enough, because we only learned some basic syntax of React, such as introducing libraries, class components and component life cycle. Next we will introduce these syntaxes in detail.

JSX syntax

React uses JSX to define the structure of components. In the above example, we use the ES6 class syntax, which is somewhat similar to traditional object-oriented programming (Object-Oriented Programming, OOP). But JSX is closer to HTML in JavaScript, making writing components more intuitive. For example, in the above example, the

tag and