How to do it...

Create a React application that will display a welcome message writing functional, class, and expression components:

  1. Create a new file named basics.js.
  2. Import the React and ReactDOM libraries:
      import * as React from 'react' 
      import * as ReactDOM from 'react-dom' 
  1. Define a new functional component that will render a span React element with color set to red in its style attributes:
      const RedText = ({ text }) => ( 
          <span style={{ color: 'red' }}> 
              {text} 
          </span> 
      ) 
  1. Define another functional component that will render an h1 React element and the RedText functional component as part of its children:
      const Welcome = ({ to }) => ( 
          <h1>Hello, <RedText text={to}/></h1> 
      ) 
  1. Define an expression that will contain a reference to a React element:
      const TodoList = ( 
          <ul> 
              <li>Lunch at 14:00 with Jenny</li> 
              <li>Shower</li> 
          </ul> 
      ) 
  1. Define a class component named Footer that will display the current date:
      class Footer extends React.Component { 
          render() { 
              return ( 
                  <footer> 
                      {new Date().toDateString()} 
                  </footer> 
              ) 
          } 
      } 
  1. Render the application to the DOM:
      ReactDOM.render( 
          <div> 
              <Welcome to="John" /> 
              {TodoList} 
              <Footer /> 
          </div>, 
          document.querySelector('[role="main"]'), 
      ) 
  1. Save the file.

Then, create an index.html file where you will render the React application:

  1. Create a new file named index.html
  2. Add the following code:
      <!DOCTYPE html> 
      <html lang="en"> 
      <head> 
          <meta charset="UTF-8"> 
          <title>MyApp</title> 
      </head> 
      <body> 
          <div role="main"></div> 
          <script src="./basics.js"></script> 
      </body> 
      </html> 
  1. Save the file
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.9.138