7.4 Introduction to ReactJS

  1. Motivations
    • React is an open-source web UI framework (library) created by Facebook in 2013. It quickly became one of the most popular frontend frameworks for UI development. It is adored by web developers because it requires minimal effort in coding and its ability to provide an outstanding user experience, making it one of the best UI frameworks of all time.
    • Programming languages used: JavaScript/JSX (HTML+JavaScript syntax)
    • An example from https://reactjs.org/docs/add-react-to-a-website.html; Click the next button.

  2. Introduction to React
    • When should you use React (Reference: https://www.communicationcrafts.com/frontend-frameworks-for-web-development-in-2021/)
      • Single page or cross-platform application development
      • Data visualization tools
      • Social networks
      • Web apps
      • Messaging apps
      • Blogs (Personal or professional)
    • PROS of React (Reference: https://www.communicationcrafts.com/frontend-frameworks-for-web-development-in-2021/)
      • Easy to learn and use: Developers familiar with JavaScript can learn to use React in a matter of days. 69.7% of developers prefer JavaScript based frameworks over other programming languages.
      • Virtual Direct Object Model (DOM): The tree-like structure of an HTML program is known as DOM. The users' actions on a website trigger an update that rewrites the entire DOM- requiring more resources. A virtual DOM works more efficiently because it changes only the elements that need manipulation.
      • Requires less coding: Easy to create highly functional web UIs
      • Reusability: React has reusable HTML codes that make a developer's life quite easy. It is easier to make and maintain web UIs because of this feature.
      • Downward data flow: React framework's data flow is relatively simple. It has a straightforward downward data flow that is easy to track and work with.
    • CONS of React (Reference: https://www.communicationcrafts.com/frontend-frameworks-for-web-development-in-2021/)
      • Continuous updates: Developers have to constantly re-learn new ways of coding because web UI the framework changes at a very rapid pace.
      • Poor documentation: The extension libraries are not efficiently updated by the supporting community.
      • JSX complications: React uses syntax that is a combination of HTML and JavaScript (Note: a new data type for HTML code). It has a steep learning curve and is difficult for amateur developers to work with.

  3. Examples
    • An example from https://reactjs.org/docs/add-react-to-a-website.html
      <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
      <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
      <div id="like_button_container">
      </div>
      <!-- Load our React component. -->
      <script src="like_button.js"></script>
      
      <!-- like_button.js
      'use strict';
      
      const e = React.createElement;
      
      class LikeButton extends React.Component {
        constructor(props) {
          super(props);
          this.state = { liked: false };
        }
      
        render() {
          if (this.state.liked) {
            return 'You liked this.';
          }
      
          // JS
          return e(
            'button',
            { onClick: () => this.setState({ liked: true }) },
            'Like'
          );
          /* or
          // JSX
          return (
            <button onClick={() => this.setState({ liked: true })}>
              Like
            </button>
          );
          */
        }
      }
      
      const domContainer = document.querySelector('#like_button_container');
      ReactDOM.render(e(LikeButton), domContainer);
      -->
      
    • Try the above example!
    • Optionally Babel for JSX can be used. https://reactjs.org/docs/add-react-to-a-website.html#optional-try-react-with-jsx
      <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
      <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
      <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
      <div id="like_button_container_jsx"></div>
      <!-- Load our React component. -->
      <script type="text/babel" src="like_button_jsx.js"><!-- See the above code for this example. -->
      
    • Hello World! from https://reactjs.org/docs/hello-world.html
      <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
      <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
      <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
      <div id='hello-world-container'></div>
      <script type="text/babel">
          ReactDOM.render(
            <h1>Hello, world!</h1>,
            document.getElementById('hello-world-container')
          );
      </script>
      

  4. Introduction to JSX from https://reactjs.org/docs/introducing-jsx.html

  5. Tutorial from https://reactjs.org/tutorial/tutorial.html

  6. React Tutorial from w3schools.com
    • React Get Started
      • React directly in HTML with Babel, or
      • Setting up a React environment so that JSX can be executed at the server side
        • $ npx create-react-app my-react-app
        • $ cd my-react-app
        • In package.json, "start": "react-scripts start" -> "start": "PORT=yourport react-scripts start"
        • $ npm start -> http://cs.tru.ca:yourport (The port 8099 is currently running.)
        • Modify the React application: src -> App.js
        • Later, $ npm run build
    • React uses ES6. Let's learn Template Literals, ES6 Classes, Array method - .map(), Destructuring, and Modules.
      • Here it an example of .map(), arrow function, and template literal.
        const data = ['apple', 'banana', 'orange'];
        const list = data.map((item) => `<li>${item}</li>`);
        //const list = data.map(function(item) { return `<li>${item}</li>`; });
        alert(list);
        
      • Try this for React and .map()!
      • Trial 1. Let's try the above code.
    • React Render HTML: React renders HTML to the web page.
    • React JSX: JSX allows us to write HTML elements in JavaScript.
      const myelement = <h1>I Love JSX!</h1>;  // It is an example of JSX. One top level HTML element.
      ReactDOM.render(myelement, document.getElementById('root'));
      
    • React Components: Components are like functions that return HTML elements.
      • Trial 2. Let's try the next code.

  7. Learning outcomes