A Beginner's Guide to React

React is a JavaScript library for building user interfaces. If you're new to React, this guide will help you get started with the basics. We'll cover the fundamental concepts of React, including components, state, and props, and include examples of how to use React for common tasks such as rendering dynamic lists and handling user input.

What is React?

React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It was developed by Facebook, and is often used in combination with other libraries or frameworks, such as Redux and React Router. React allows you to build reusable UI components, manage the state of your application, and efficiently update the UI in response to user actions.

Getting Started with React

To start using React, you will need to have a recent version of Node.js and the Node.js package manager (npm) installed on your machine. You can then create a new React project using the Create React App tool by running the following command:

npx create-react-app my-app

This will create a new directory called "my-app" with a basic React setup, including a development server and scripts for building and testing the app. You can then navigate to the project directory and start the development server by running the following commands:

cd my-app npm start

This will open a new browser window with the default React app, which displays a message saying "Edit src/App.js and save to reload." You can edit the app by modifying the code in the "src" directory and the changes will be reflected in the browser as you save the files.

Components

In React, a component is a piece of UI that can be reused throughout an app. Components are typically defined as functions or classes that return a React element. Here is an example of a functional component that displays a simple message:

import React from 'react';
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}

This component takesa single prop, name, and returns a React element that displays a greeting with the name. You can use this component in another component or in the root component of your app by importing it and using the JSX syntax to include it in the render method:

import React from 'react';
import Greeting from './Greeting';
function App() {
return (
<div>
<Greeting name="John" />
<Greeting name="Jane" />
<Greeting name="Joe" />
</div>
);
}

This will render three greetings, one for each name provided as a prop. You can also define components as classes that extend the React.Component base class. Here is an example of a class-based component that displays a list of items:

import React, { Component } from 'react';
class ItemList extends Component {
  render() {
    return (
      <ul>
        {this.props.items.map(item => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    );
  }
}

This component takes a prop called items, which is an array of objects. It uses the map function to render a list item for each object in the array. The key prop is used to uniquely identify each list item, which is important for performance when rendering dynamic lists.

State and Props

In React, props are used to pass data from a parent component to a child component, while state is used to store and manage data within a component. Props are essentially read-only, while state can be modified by the component itself. Here is an example of a component that has state and uses it to toggle the visibility of a message:

import React, { Component } from 'react';
class ToggleMessage extends Component {
  state = {
    visible: false
  };

  handleClick = () => {
    this.setState(CodeBlockvState => ({
      visible: !CodeBlockvState.visible
    }));
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Toggle Message</button>
          {this.state.visible && (
           <p>{this.props.message}</p>
          )}
      </div>
    );
  }
}

This component has a state variable called visible that determines whether the message is shown or hidden. It has a click event handler that toggles the value of visible when the button is clicked. It also renders the message if visible is true, using the JSX syntax for a conditional rendering. The message itself is passed as a prop called message. You can use this component in another component or in the root component of your app by passing a value for the message prop:

import React from 'react';
import ToggleMessage from './ToggleMessage';
function App() {
  return (
    <div>
     <ToggleMessage message="Hello, World!" />
    </div>
  );
}

This will render a button and a message that can be toggled by clicking the button. You can also pass props from a parent component to a child component using the JSX syntax. For example:

import React from 'react';
import ToggleMessage from './ToggleMessage';
  function App() {
    return (
      <div>
       <ToggleMessage message="Hello, World!" />
        <ToggleMessage message="Welcome to React!" />
      </div>
  );
}

This will render two instances of the ToggleMessage component, each with a different message prop. You can also pass props from a child component to a parent component using the props.children prop and the JSX syntax for rendering children. For example:

import React from 'react';
function Card(props) {
  return (
    <div className="card">
     {props.children}
    </div>
  );
  }

  function App() {
    return (
      <div>
        <Card>
          h2>Title</h2 >
         <p>Content</p>
        </Card>
      </div>
    );
}

This will render a card component with a title and content, passed as children to the component. The children will be rendered inside the div element with the className of "card".

Handling User Input

React allows you to handle user input using form elements, such as input, select, and textarea. You can use the value and onChange props to control the state of the form and bind it to the input value. Here is an example of a form component that manages its own state and updates the value of an input field:

import React, { Component } from 'react';
class Form extends Component {
  state = {
    inputValue: ''
  };
  handleInputChange = event => {
      this.setState({ inputValue: event.target.value });
    }
  render() {
    return (
      <form>
        <input
        type="text"
        value={this.state.inputValue}
        onChange={this.handleInputChange}
        />
      </form>
    );
}
}

This component has a state variable called inputValue that stores the value of the input field. It has an event handler called handleInputChange that updates the value of inputValue when the input field is changed. The value and onChange props are used to bind the input field to the state variable and update it when the user types. You can use this component in another component or in the root component of your app by including it in the JSX:

import React from 'react';
import Form from './Form';
function App() {
  return (
    <div>
     <Form />
    </div>
  );
}

This will render a form with an input field that you can type in. You can also submit the form by adding a submit button and a submit event handler:

import React, { Component } from 'react';
class Form extends Component {
  state = {
   inputValue: ''
  };

  handleInputChange = event => {
   this.setState({ inputValue: event.target.value });
  }

  handleSubmit = event => {
    event.CodeBlockventDefault();
    // Send the input value to the server or do something else here
  }

render() {
  return (
    <form onSubmit={this.handleSubmit}>
      <input
      type="text"
      value={this.state.inputValue}
      onChange={this.handleInputChange}
      />
      <button type="submit">Submit</button>
    </form>
  );
}
}

This will add a submit button to the form and a submit event handler that CodeBlockvents the default form submission behavior and can be used to send the input value to the server or perform some other action. You can also use the select element and the option element to create a dropdown menu and bind it to the state:

import React, { Component } from 'react';
class Form extends Component {
  state = {
    selectValue: 'option1'
  };

  handleSelectChange = event => {
   this.setState({ selectValue: event.target.value });
  }

  render() {
    return (
      <form>
      <select value={this.state.selectValue} onChange={this.handle.SelectChange}>
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>
      </select>
      </form>
    );
  }
}

This will create a dropdown menu with three options and bind the selected option to the state variable selectValue. You can also use the textarea element to create a textarea and bind it to the state:

import React, { Component } from 'react';
class Form extends Component {
  state = {
   textareaValue: ''
  };

  handleTextareaChange = event => {
   this.setState({ textareaValue: event.target.value });
  }

  render() {
    return (
      <form>
       <textarea value={this.state.textareaValue} onChange={this.handleTextareaChange} />
      </form>
    );
  }
}

This will create a textarea and bind the value to the state variable textareaValue. You can then use the state variables to access the user input and use it in your app.

Rendering Dynamic Lists

React allows you to render dynamic lists using the map function and the JSX syntax for rendering lists. You can use the key prop to uniquely identify each list item, which is important for performance when rendering dynamic lists. Here is an example of a component that renders a list of items:

import React from 'react';
function ItemList(props) {
  return (
    <ul>
      {props.items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

This component takes a prop called items, which is an array of objects. It uses the map function to render a list item for each object in the array. The key prop is used to uniquely identify each list item, which is important for performance when rendering dynamic lists. You can use this component in another component or in the root component of your app by passing an array of items as the items prop:

import React from 'react';
import ItemList from './ItemList';
  const items = [
    { id: 1, name: 'Item 1' },
    { id: 2, name: 'Item 2' },
    { id: 3, name: 'Item 3' }
  ];

  function App() {
    return (
      <div>
      <ItemList items={items} />
      </div>
      );
      }

This will render a list of three items, each with a unique id and a name. You can also use the map function to render more complex list items, such as components with props:


      import React from 'react';
      function Item(props) {
      return (
      <li>
      <strong>{props.item.name}</strong>: {props.item.description}
      </li>
    );
  }

  function ItemList(props) {
  return (
  <ul>
  {props.items.map(item => (
  <Item key={item.id} item={item} />
  ))}
  </ul>
  );
}

This will render a list of items, each with a name and a description. You can also use the map function to render a list of components with state and event handlers, such as a to-do list:

import React, { Component } from 'react';
class ToDoItem extends Component {
state = {
done: false
};

handleClick = () => {
this.setState(CodeBlockvState => ({
done: !CodeBlockvState.done
}));
}

render() {
return (
<li onClick={this.handleClick} style={{ textDecoration: this.state.done ? 'line-through' : 'none' }}>
{this.props.item.name}
</li>
);
}
}

class ToDoList extends Component {
render() {
return (
<ul>
{this.props.items.map(item => (
<ToDoItem key={item.id} item={item} />
))}
</ul>
);
}
}

This will render a list of to-do items that can be marked as done by clicking on them. The done state variable is used to toggle the line-through style of the list items, and the handleClick event handler is used to toggle the value of done. You can use this component in another component or in the root component of your app by passing an array of items as the items prop:

import React from 'react';
import ToDoList from './ToDoList';
const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
];

function App() {
  return (
    <div>
      <ToDoList items={items} />
    </div>
  );
}

This will render a list of to-do items that can be marked as done by clicking on them. You can use the map function and the JSX syntax for rendering lists to create dynamic lists of any kind in React.

Summary

React is a powerful JavaScript library for building user interfaces. It allows you to create reusable components, manage state and props, handle user input, and render dynamic lists. By learning the basics of React, you can build efficient and scalable front-end applications that can run on the web, mobile, or server.

Share it if you like it 😉

© 2024 Khannoussi Malek, Inc. All rights reserved.

HTML🎨CSSJavaScript🌠✨TypeScript✨PHP⚛️React🔥Jhipster NodeJS⚡️Chakra-UiReact-queryLumen🦊 Gitlab 🦊