Skip to main content

Understanding Components


In this tutorial, we are going to explore the fundamental building blocks of any React application: Components. If you are new to React, don't worry. We will start from the basics and gradually delve into more complex concepts, making sure everything is clear and easy to follow.

What is a Component?

In React, a component is a reusable piece of code that is responsible for rendering a part of the user interface (UI). Think of components as Lego blocks. Just like you can combine Lego blocks to form complex structures, you can compose React components to build complex UIs.

Types of Components

There are two main types of components in React:

  1. Functional Components: These are simple JavaScript functions. We call them 'functional' because they simply receive an input (props) and return what to display (JSX).

    function Welcome(props) {
    return <h1>Hello, {props.name}</h1>;
    }
  2. Class Components: These are more complex and provide more features. They are ES6 classes that extend from React.Component.

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

Props

Props (short for properties) are how components talk to each other. They are the inputs to a component and they are passed down from the parent to the child. In the examples above, name is a prop.

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

ReactDOM.render(
<Welcome name="Sara" />,
document.getElementById('root')
);

In this example, we pass the name prop to the Welcome component. Inside the component, we access the prop using props.name.

State

State is a feature that allows components to create and manage their own data. Unlike props, state is local to the component and it can change over time.

Here is an example of a class component with state:

class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}

render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}

In this example, date is a state variable and we can update it using the setState method.

Lifecycle Methods

Lifecycle methods are special methods that automatically get called as your component gets rendered and updated. There are several lifecycle methods, but the most commonly used ones are componentDidMount, componentDidUpdate, and componentWillUnmount.

class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}

componentDidMount() {
// Called after the component is rendered
}

componentDidUpdate() {
// Called after the component updates
}

componentWillUnmount() {
// Called before the component is removed
}

render() {
// ...
}
}

Now that you are familiar with the basics of React components, you should feel more comfortable diving into more complex topics. Remember, components are the heart of React, so understanding them is crucial for mastering React. Happy coding!