02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

001.005.Building our first JSX React Component

Congratulations on reaching this pivotal stage! In this tutorial, you’ll build your first JSX-based React component and integrate it into your project. Along the way, you’ll explore the nuances of JSX, set up React libraries, and understand how React and JSX work behind the scenes. By the end, you’ll have a functioning React component rendering directly to the DOM.


Steps to Build a JSX React Component

1. Understanding React and JSX

React components are the core building blocks of React applications. These components use JSX (JavaScript XML), a syntax extension that resembles HTML but works seamlessly with React. JSX makes it easier to visualize and manage your UI structure within JavaScript files.


2. Install React and ReactDOM

To create and render React components, you need two libraries:

  • React: Creates and manages the components.
  • ReactDOM: Handles rendering components to the DOM.

Run the following command to install these libraries:

npm install react react-dom

Note: Unlike development tools, React libraries should be installed as production dependencies (without --save-dev) because they are required when your app runs.


3. Set Up Your Component

  1. Import React and ReactDOM
    Add the following imports to your JavaScript file to use React and ReactDOM:

    import React from 'react';  
    import ReactDOM from 'react-dom';  
    
  2. Prepare JSX for Your Component
    JSX is syntactically similar to HTML. To build your first component:

    • Open your HTML file and copy the relevant markup (e.g., a Bootstrap Jumbotron).
    • Paste this markup into your JavaScript file.

    Example:

    const MyComponent = () => {  
      return (  
        <div className="jumbotron">  
          <h1>Welcome to React!</h1>  
          <p>This is your first JSX React component.</p>  
          <button className="btn btn-primary">Click Me</button>  
        </div>  
      );  
    };  
    

    Important: In JSX, use className instead of class (a reserved keyword in JavaScript).

  3. Render Your Component to the DOM
    Use ReactDOM.render() to render the component into a specific element in your HTML file.

    • Add a div with an id in your HTML:
      <div id="root"></div>  
      
    • In your JavaScript, render your component:
      ReactDOM.render(<MyComponent />, document.getElementById('root'));  
      

4. Handling Inline Styles in JSX

In JSX, inline styles are defined as objects, and property names follow camelCase conventions. For example:

const MyStyledComponent = () => {  
  return (  
    <div  
      style={{  
        backgroundColor: 'lightblue',  
        padding: '20px',  
        textAlign: 'center'  
      }}  
    >  
      <h1 style={{ color: 'darkblue' }}>Styled Component</h1>  
    </div>  
  );  
};  

5. Test Your Component

Start your development server:

npm start

Open your browser and verify that your React component is rendering as expected.


Key Takeaways

  1. JSX Basics: JSX allows you to write HTML-like syntax in your JavaScript files. It must have a single root element (e.g., a div) and use className for CSS classes.
  2. ReactDOM: This library bridges the gap between React components and the actual DOM.
  3. Inline Styling in JSX: Inline styles must use JavaScript objects and camelCase property names.
  4. Debugging JSX: Errors in JSX often relate to syntax or reserved keywords (like class or for).

Next Steps

This tutorial marks your first steps into React development. In the next section, you’ll dive deeper into creating reusable React components, managing state, and exploring ES6 features like destructuring and arrow functions.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

Setting up our foundation project

Learn to set up a ReactJS project using Node.js, NPM, and Webpack. Prepare for dynamic, responsive interfaces with Bootstrap and SaaS.

14:36

Setting up Webpack and WebPack-Dev-Server

Learn how to configure Webpack and Webpack-Dev-Server for ReactJS. Set up a local development server with hot reloading for seamless workflows.

08:19

Bootstrapping our HTML

Learn to enhance HTML with Bootstrap 4.0. Create responsive designs using Jumbotron and button components for modern visuals.

07:23

Adding Support for ES6 and JSX with BabelJS

Learn to set up BabelJS with Webpack to support ES6 and JSX. This tutorial covers loaders, presets, and polyfills for better browser compatibility.

10:55

001.005.Building our first JSX React Component

Learn how to create your first JSX React component, including setup of React, ReactDOM, and integration with JSX.

13:51