React - ILLYAKO/mywiki GitHub Wiki

React (also known as React.js or ReactJS) is a free and open-source front-end JavaScript library for building user interfaces based on components. It is maintained by Meta (formerly Facebook) and a community of individual developers and companies. React can be used to develop single-page, mobile, or server-rendered applications with frameworks like Next.js. Because React is only concerned with the user interface and rendering components to the DOM, React applications often rely on libraries for routing and other client-side functionality.

0. Preparing the Development Environment.

0.1. Installing Node.js

Download and install Node.js from https://nodejs.org/en

0.1.1. Checking the Node Version in CMD

node -v

0.1.2. Checking NPM in CMD

npm -v

0.1.3. Installing the create-react-app Package(Optional)

npm install --global create-react-app

0.2. Installing Git

Download and install from https://git-scm.com/downloads

0.2.1. Installing Git on Linux

sudo apt-get install git

0.2.2. Checking Git version

git -v

0.3. Choosing editor

  • Sublime Text
  • Atom
  • Brackets
  • Visual Studio Code
  • Visual Studio

0.4. Choosing browser

  • Chrome, react-devtools extension
  • Firefox, react-devtools extension

1. Creating the Project

1.0.1. Creating the Project

npx create-react-app myprojectname

1.0.2. Project Structure

myproject
    ┗╸.git
    ┗╸node_module
    ┗╸public
         favicon.ico
         index.html
         manifest.json
    ┗╸src
         App.css
         App.js
         App.test.js
         index.css
         index.js
         logo.svg
         registerServiceWorker.js
    .gitignore
    package.json
    package-lock.json
    README.md
1.0.2.1. Important Files in the Project
  • public/index.html
    This is the HTML file that is loaded by the browser. It contains an element in which the application is displayed and a script element that loads the application's JavaScript files.
  • src/index.js
    This is the JavaScript file that is responsible for configuring and starting the React application. The file is used to add the Bootstrap CSS framework to the application in the next section.
  • src/App.js
    This is the React component, which contains the HTML content that will be displayed to the user and the JavaSctipt code required by the HTML. Components are the main building blocks in a React application, and you will see them used throughout this book.

1.0.3. Adding the Bootstrap CSS Framework

1.0.3.1 Installing Bootstrap

cd myproject
npm install bootstrap

1.0.3.2 Including Bootstrap in the index.js file in the src folder
...
import 'bootstrap/dist/css/bootstrap.css';
...

1.0.4. Starting Development Tools

npm start

1.0.5. Running React App example in the browser

http://localhost:3000

1.0.6. Replacing the Placeholder Content in App.js

import "./App.css";
function App() {
    return (
        <div>
            <h4 className="bg-primary text-white text-center p-2">
                To-Do List
            </h4>
        </div>
    );
}
export default App;

The App.js file contains a React component, which is named App. Components are the main building block for React applications, and they are written using JSX, which is a superset of JavaScript that allows HTML to be included in code files without requiring any special quoting.

1.0.7. Displaying Dynamic Content

All web applications need to display dynamic content to the user, and React makes this easy by supporting the expressions feature. An expression is a fragment of JavaScript that is evaluated when a component's render method is called and provides the means to display data to the user. Many expressions are used to display data values defined by the component to keep track of the state of the application, known as state data.

1.0.7.1. Adding State Data and Data Bindings in the src/App.js
import React, { Component } from "react";
export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            userName: "Adam",
        };
    }
    render() {
        return (
            <div>
                <h4 className="bg-primary text-white text-center p-2">
                    {this.state.userName}'s To-Do List.
                </h4>
            </div>
        );
    }
}

The constructor is a special method that is invoked when the component is initialized, and calling the super method within the constructor is required to ensure that the component is set up properly.
The props parameter defined by the constructor is important in React development because it allows one component to configure another. The term props is short for properties, and it reflects the way React creates the HTML content that is displayed in the browser.
React components have a special property named state, which is used to define state date:

...
this.data = {
   userName: "Adam"
}
...

The this keyword refers to the current object and is used to access its properties and methods.

...
<h4 className="bg-primary text-white text-center p-2">
    {this.state.userName}'s To-Do List.
</h4>
...

Expressions are denoted with curl braces { }. When render method is invoked, the expression is evaluated, and its result is included in the content presented to the user. The expression reads the value of the userName state data property, producing the result.

1.0.7.2. Understanding State Data Changes

The dynamic nature of a React application is based on changes to state data, which React respond to by invoking the component's render method again, which causes the expressions to be re-evaluated using the new state data value.

import React, { Component } from "react";
export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            userName: "Adam",
        };
    }

    changeStateData = () => {
        this.setState({
            userName: this.state.userName === "Adam" ? "Bob" : "Adam",
        });
    };

    render() {
        return (
            <div>
                <h4 className="bg-primary text-white text-center p-2">
                    {this.state.userName}'s To-Do List.
                </h4>
                <button
                    className="btn btn-primary m-2"
                    onClick={this.changeStateData}
                >
                    Change
                </button>
            </div>
        );
    }
}

The onClick attribute is assigned an expression that React evaluates when the button is clicked. Clicking a button triggers an event, and onClick is an example of an event-handler prop. The function or method that is specified by onClick will be invoked each time the button is clicked. The expression specifies the changeStateData method, which is defined using the fat arrow syntax, which allows functions to be expressed concisely.
this.setState() set a new value for the variable and React invokes the render method to update the component.

⚠️ **GitHub.com Fallback** ⚠️