Introduction to React and Components
Tags: webdev, beginners, javascript, react
Published: 2023-06-22 15:02:19

**Audience:** This article is for beginners familiar with JavaScript looking to learn React, as well as those seeking a refresher on React components.

**Overview**

The article introduces React components, discussing their types, creation, composition, and reusability. It further explains how components communicate via 'props' and manage internal data using 'state'. Finally, it explores component lifecycle methods in React, enhancing the understanding of building dynamic applications.

React is a popular JavaScript library for building user interfaces, developed by Facebook. It focuses on the view layer, providing developers with a lot of flexibility and performance gains. This tutorial aims to help you understand the basics of React, especially its fundamental building blocks - components.

## **What are Components?**

Components are the cornerstone of React applications. They are reusable pieces of code that return a React element to be rendered onto the DOM. Components simplify the construction of large-scale applications by breaking the UI into smaller, reusable pieces. There are two main types of components in React: **Functional Components** and **Class Components**.

## **Creating Functional Components in React**

Functional components are JavaScript functions. They accept an input, called 'props', and return what should be rendered. Here's a simple example:

```javascript
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}
```

This is a functional component named 'Welcome'. It accepts one argument, 'props', and returns a React element. This element is a tree of React components that will eventually render HTML.

## **Creating Class Components in React**

Class components are more complex than functional ones. They are declared as JavaScript classes and extend from `React.Component`. Class components require a `render()` method, which is where you define what the component renders:

```javascript
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}
```

This 'Welcome' component works the same way as the functional component above. Both are valid and you can use either depending on your preference or use case.

## **Understanding Component Composition and Reusability in React**

One of the main benefits of using components is reusability. You can define a component once and reuse it in multiple places. Additionally, you can compose components to create complex UIs.

For example, you can create an 'App' component that renders the 'Welcome' component:

```javascript
function App() {
  return (
    <div>
      <Welcome name="Alice" />
      <Welcome name="Bob" />
    </div>
  );
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);
```

In the example above, we have reused the 'Welcome' component twice. The 'App' component is a parent component, while the 'Welcome' component is a child. This represents a 'composition', which is a key pattern in React.

## **Passing Props to Components in React**

'Props' is short for properties. They are how components talk to each other. Props are passed from the parent component to the child component and are read-only for the child component.

```javascript
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

function App() {
  return (
    <div>
      <Welcome name="Alice" />
      <Welcome name="Bob" />
    </div>
  );
}
```

In this example, we are passing a 'name' prop from the 'App' component to the 'Welcome' component. The 'Welcome' component then uses this prop in its render output.

## **Handling Component State in React**

While props allow child components to receive data from their parent, the state allows components to manage and update their own data. State is similar to props, but it is private and fully controlled by the component.

Class components can have local state:

```javascript
class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={this.increment}>
          Click me
        </button>
      </div>
    );
  }
}

ReactDOM.render(
  <Counter />,
  document.getElementById('root')
);
```

In the example above, the 'Counter' class component maintains its count state. When the 'Click me' button is pressed, the 'increment' function is called, which updates the component's state using the `setState()` function.

## **Managing Component Lifecycle in React**

Class components in React have built-in methods that run at different points in a component's lifecycle. These lifecycle methods give you the ability to control what happens when a component mounts, updates, and unmounts.

The most commonly used lifecycle methods are `componentDidMount()`, `componentDidUpdate()`, and `componentWillUnmount()`.

Here is an example:

```javascript
class Example extends React.Component {
  componentDidMount() {
    console.log('Component mounted');
  }

  componentDidUpdate() {
    console.log('Component updated');
  }

  componentWillUnmount() {
    console.log('Component will unmount');
  }

  render() {
    return <h1>Hello, world!</h1>;
  }
}
```

In this 'Example' class component, we are using lifecycle methods to log messages when the component mounts, updates, and unmounts.

To summarize, React components are a core concept when building applications with React. Understanding how to create and manipulate these components and their lifecycle will allow you to create complex and powerful applications with ease. It is recommended to practice and experiment with components, as experience will help solidify these concepts.