Introduction to React Components
In React, components are the building blocks of a user interface. They represent a piece of the UI that can be reused throughout the application.
Just like LEGO bricks can be combined in various ways to create different structures, React components can be combined to create complex UIs.
Components in React can be of two types: functional components and class components. Functional components are defined as JavaScript functions, while class components are defined as ES6 classes that extend the React.Component
class.
Here's an example of a functional component in React:
1import React from 'react';
2
3function Greeting() {
4 return (
5 <div>
6 <h2>Hello, World!</h2>
7 <p>This is a functional component.</p>
8 </div>
9 );
10}
11
12export default Greeting;
This component is a simple greeting message that can be rendered anywhere in the application.
Functional components are easier to write, understand, and test. They are also recommended for most scenarios in React development. However, there are cases where class components are still useful, especially when working with complex state management or lifecycle methods.
xxxxxxxxxx
// React component example
import React from 'react';
function App() {
return (
<div>
<h1>Welcome to AlgoDaily!</h1>
<p>Learn and explore frontend development.</p>
</div>
);
}
export default App;
Build your intuition. Click the correct answer from the options.
Which of the following is true about React components?
Click the option that best answers the question.
- Functional components are defined as ES6 classes that extend the React.Component class
- Components are the building blocks of a user interface in Angular
- Components can be combined to create complex user interfaces in React
- Class components are recommended for most scenarios in React development
Creating Functional Components
In React, functional components are created as JavaScript functions. They are a simpler and more concise way of defining components compared to class components.
Functional components are especially useful when you only need to render UI based on the given props and don't require any internal state or lifecycle methods.
Here's an example of a functional component:
1function Greeting() {
2 return (
3 <div>
4 <h2>Hello, World!</h2>
5 <p>This is a functional component.</p>
6 </div>
7 );
8}
In this example, the Greeting
component simply renders a div
element containing a heading and a paragraph. This component can be reused and rendered anywhere in your application.
With functional components, you can also utilize React hooks such as the useState
hook to introduce state management into your components.
xxxxxxxxxx
// In React, functional components are created as JavaScript functions
function Greeting() {
return (
<div>
<h2>Hello, World!</h2>
<p>This is a functional component.</p>
</div>
);
}
Build your intuition. Is this statement true or false?
Creating functional components requires the use of class syntax.
Press true if you believe the statement is correct, or false otherwise.
Using Props to Pass Data
One of the key concepts in React is props. Props (short for properties) allow you to pass data from a parent component to a child component.
Let's take a look at an example:
1// Let's create a simple parent component
2
3function ParentComponent() {
4 const greeting = "Hello, World!";
5 return (
6 <div>
7 <ChildComponent greeting={greeting} />
8 </div>
9 );
10}
11
12// Now let's create the child component
13
14function ChildComponent(props) {
15 return (
16 <div>
17 <h2>{props.greeting}</h2>
18 <p>This is a child component.</p>
19 </div>
20 );
21}
22
23// In this example, the ParentComponent passes the greeting
24// value as a prop called 'greeting' to the ChildComponent.
25
26// The ChildComponent receives the prop and renders it in
27// a heading element. The prop can be accessed using the 'props'
28// parameter passed to the functional component.
29
30// This way, data can be passed from the parent component to
31// the child component using props.
In the code snippet above, we have a ParentComponent
that renders a ChildComponent
. The ParentComponent
has a variable called greeting
with the value "Hello, World!".
The greeting
value is passed to the ChildComponent
as a prop named greeting
.
Inside the ChildComponent
, the prop value is accessed using the props
parameter passed to the function. The prop is then rendered inside a heading element.
By using the props, we can pass data from the parent component to the child component and utilize it to render dynamic content or perform other operations.
xxxxxxxxxx
// the child component using props.
// Let's create a simple parent component
function ParentComponent() {
const greeting = "Hello, World!";
return (
<div>
<ChildComponent greeting={greeting} />
</div>
);
}
// Now let's create the child component
function ChildComponent(props) {
return (
<div>
<h2>{props.greeting}</h2>
<p>This is a child component.</p>
</div>
);
}
// In this example, the ParentComponent passes the greeting
// value as a prop called 'greeting' to the ChildComponent.
// The ChildComponent receives the prop and renders it in
// a heading element. The prop can be accessed using the 'props'
// parameter passed to the functional component.
Try this exercise. Is this statement true or false?
Props allow you to pass data from a parent component to a child component.
Press true if you believe the statement is correct, or false otherwise.
Rendering Components Conditionally
In React, you can render components conditionally based on certain conditions or states. This allows you to show different components or content depending on the current state of your application.
For example, let's say you have a simple application where a user can either be logged in or be a guest. You want to display a personalized greeting if the user is logged in, and a signup prompt if the user is a guest.
Here's an example of how you can conditionally render components based on the value of a prop:
1function UserGreeting() {
2 return (
3 <div>
4 <h1>Welcome back!</h1>
5 <button onClick={logout}>Logout</button>
6 </div>
7 );
8}
9
10function GuestGreeting() {
11 return (
12 <div>
13 <h1>Please sign up.</h1>
14 <button onClick={login}>Login</button>
15 </div>
16 );
17}
18
19function Greeting(props) {
20 const isLoggedIn = props.isLoggedIn;
21
22 if (isLoggedIn) {
23 return <UserGreeting />;
24 }
25 return <GuestGreeting />;
26}
27
28function App() {
29 const [isLoggedIn, setIsLoggedIn] = useState(false);
30
31 const login = () => {
32 setIsLoggedIn(true);
33 };
34
35 const logout = () => {
36 setIsLoggedIn(false);
37 };
38
39 return (
40 <div>
41 <Greeting isLoggedIn={isLoggedIn} />
42 </div>
43 );
44}
45
46ReactDOM.render(
47 <React.StrictMode>
48 <App />
49 </React.StrictMode>,
50 document.getElementById('root')
51);
In this example, we have three components: UserGreeting
, GuestGreeting
, and Greeting
.
UserGreeting
component displays a welcome message and a button to logout.GuestGreeting
component displays a signup prompt and a button to login.Greeting
component checks theisLoggedIn
prop and renders either theUserGreeting
orGuestGreeting
component accordingly.
The parent App
component holds the isLoggedIn
state and passes it to the Greeting
component as a prop.
When the login
button is clicked, the isLoggedIn
state is set to true
, and the UserGreeting
component is rendered.
When the logout
button is clicked, the isLoggedIn
state is set to false
, and the GuestGreeting
component is rendered.
By rendering components conditionally, you can provide a personalized user experience based on the current state or conditions of your application.
xxxxxxxxxx
);
// Let's create a simple example
function UserGreeting() {
return (
<div>
<h1>Welcome back!</h1>
<button onClick={logout}>Logout</button>
</div>
);
}
function GuestGreeting() {
return (
<div>
<h1>Please sign up.</h1>
<button onClick={login}>Login</button>
</div>
);
}
// Create a component that determines whether
// to render UserGreeting or GuestGreeting based on
// the value of the isLoggedIn prop.
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return <UserGreeting />;
Let's test your knowledge. Click the correct answer from the options.
What is a benefit of rendering components conditionally?
Click the option that best answers the question.
- Improved performance by reducing unnecessary rendering
- Enhanced user experience by providing personalized content
- Easier maintenance and code organization
- All of the above
Handling Events in Components
In React, you can handle user interactions and events by adding event handlers to your components. Event handlers are functions that are executed when a certain event occurs, such as a button click or a form submission.
Let's create a simple button component that triggers an event when clicked:
1function Button(props) {
2 const handleClick = () => {
3 // Handle the click event here
4 console.log('Button clicked!');
5 };
6
7 return (
8 <button onClick={handleClick}>{props.label}</button>
9 );
10}
11
12function App() {
13 return (
14 <div>
15 <h1>Handling Events in Components</h1>
16 <p>In React, you can handle user interactions and events by adding event handlers to your components. Let's create a simple button component that triggers an event when clicked.</p>
17 <Button label="Click me!" />
18 </div>
19 );
20}
21
22ReactDOM.render(
23 <React.StrictMode>
24 <App />
25 </React.StrictMode>,
26 document.getElementById('root')
27);
In this example, we have a Button
component that takes a label
prop. Inside the component, we define a function handleClick
that gets executed when the button is clicked. The handleClick
function simply logs a message to the console.
The button element has an onClick
attribute that is assigned the handleClick
function. This binds the function as the event handler for the button's click event.
When the button is clicked, the handleClick
function is executed, and the message 'Button clicked!' is logged to the console.
You can modify the handleClick
function to perform any desired action when the button is clicked, such as updating state, making API calls, or navigating to another page.
xxxxxxxxxx
// Let's create a simple button component that triggers an event when clicked
function Button(props) {
const handleClick = () => {
// Handle the click event here
console.log('Button clicked!');
};
return (
<button onClick={handleClick}>{props.label}</button>
);
}
function App() {
return (
<div>
<h1>Handling Events in Components</h1>
<p>In React, you can handle user interactions and events by adding event handlers to your components. Let's create a simple button component that triggers an event when clicked.</p>
<Button label="Click me!" />
</div>
);
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Are you sure you're getting this? Click the correct answer from the options.
Which of the following is NOT a best practice for handling events in React components?
Click the option that best answers the question.
- Defining event handlers inline in JSX
- Binding event handlers in the constructor
- Using arrow functions for event handlers
- Passing arguments to event handlers
Stateful Components with useState Hook
React provides the useState hook as a way to manage state in functional components. We can use the useState hook to create stateful components by defining a state variable and an update function.
The useState hook takes an initial state value as an argument and returns an array containing the current state value and a function to update the state value.
Here's an example of how to use the useState hook to create a simple counter component:
1import React, { useState } from 'react';
2
3function Counter() {
4 const [count, setCount] = useState(0);
5
6 const increment = () => {
7 setCount(count + 1);
8 };
9
10 const decrement = () => {
11 setCount(count - 1);
12 };
13
14 return (
15 <div>
16 <h2>Counter</h2>
17 <p>Count: {count}</p>
18 <button onClick={increment}>Increment</button>
19 <button onClick={decrement}>Decrement</button>
20 </div>
21 );
22}
23
24function App() {
25 return (
26 <div>
27 <h1>Stateful Components with useState Hook</h1>
28 <p>React provides the useState hook as a way to manage state in functional components. We can use the useState hook to create stateful components by defining a state variable and an update function. Let's create a simple Counter component to demonstrate this:</p>
29 <Counter />
30 </div>
31 );
32}
33
34ReactDOM.render(
35 <React.StrictMode>
36 <App />
37 </React.StrictMode>,
38 document.getElementById('root')
39);
In this example, we define a Counter
component that uses the useState hook to create a state variable count
and an update function setCount
. The initial state value is 0
.
The Counter
component renders the current value of count
and two buttons to increment and decrement the count. The increment
and decrement
functions update the count
state using the setCount
function.
We can use the useState hook multiple times in a component to create multiple independent state variables.
The useState hook ensures that the component is re-rendered whenever the state value is updated.
Feel free to experiment with the code and see how the counter component renders and updates the count value on the screen.
xxxxxxxxxx
);
// Replace with stateful component code
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
const decrement = () => {
setCount(count - 1);
};
return (
<div>
<h2>Counter</h2>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
}
function App() {
return (
<div>
<h1>Stateful Components with useState Hook</h1>
Are you sure you're getting this? Is this statement true or false?
React provides the useState hook as a way to manage state in functional components. We can use the useState hook to create stateful components by defining a state variable and an update function.
Press true if you believe the statement is correct, or false otherwise.
Lifting State Up
Lifting state up is a technique in React where the state management is moved from a child component to a parent component in order to share data between multiple child components.
To understand lifting state up, let's consider an example. Imagine we have a parent component called TemperatureConverter
that consists of two child components: CelsiusInput
and FahrenheitInput
.
The CelsiusInput
component takes a temperature in Celsius as an input, and the FahrenheitInput
component takes a temperature in Fahrenheit as an input.
The goal is to allow the user to input a temperature in one component and automatically update the other component with the converted temperature.
To accomplish this, we need to lift the state of the temperatures from the child components to the parent component. The parent component will handle the conversion logic and update the state accordingly.
Here's an example of how we can implement this in React:
1import React, { useState } from 'react';
2
3function TemperatureConverter() {
4 const [celsius, setCelsius] = useState(0);
5 const [fahrenheit, setFahrenheit] = useState(0);
6
7 const handleCelsiusChange = (event) => {
8 const value = event.target.value;
9 setCelsius(value);
10 setFahrenheit((value * 9) / 5 + 32);
11 };
12
13 const handleFahrenheitChange = (event) => {
14 const value = event.target.value;
15 setFahrenheit(value);
16 setCelsius(((value - 32) * 5) / 9);
17 };
18
19 return (
20 <div>
21 <h2>Lifting State Up</h2>
22 <div>
23 <label>Celsius:</label>
24 <input type="number" value={celsius} onChange={handleCelsiusChange} />
25 </div>
26 <div>
27 <label>Fahrenheit:</label>
28 <input type="number" value={fahrenheit} onChange={handleFahrenheitChange} />
29 </div>
30 </div>
31 );
32}
33
34export default TemperatureConverter;
In this example, the TemperatureConverter
component holds the state for both the celsius
and fahrenheit
values using the useState
hook. The component defines two event handlers, handleCelsiusChange
and handleFahrenheitChange
, to update the state based on user input.
The CelsiusInput
and FahrenheitInput
components receive the values as props and display them in their respective input fields. When the user changes the value in one input field, the corresponding event handler updates the state, which triggers a re-render of both input fields.
By lifting the state up to the TemperatureConverter
component, we have centralized the state management and ensured that both child components always display the correct converted temperature value.
Lifting state up is useful in situations where multiple components need access to the same data or when you need to synchronize the state between components. It promotes reusability and makes the code easier to understand and maintain.
xxxxxxxxxx
}
// Lifting State Up
// Lifting state up is the process of moving the state
// management from a child component to a parent component
// in order to share the data between multiple child components.
// Let's say we have a parent component called `TemperatureConverter`
// that consists of two child components: `CelsiusInput`
// and `FahrenheitInput`.
// The `CelsiusInput` component takes a temperature in Celsius
// as an input and the `FahrenheitInput` component takes a temperature
// in Fahrenheit as an input.
// The goal is to allow the user to input a temperature in one input field
// and automatically update the other input field with the converted temperature.
// To achieve this, we need to lift the state of the temperatures
// from the child components to the parent component and have the parent
// component handle the conversion logic and update the state.
// Here's an example of how this can be implemented:
import React, { useState } from 'react';
function TemperatureConverter() {
const [celsius, setCelsius] = useState(0);
const [fahrenheit, setFahrenheit] = useState(0);
Let's test your knowledge. Is this statement true or false?
Lifting state up is the technique of moving the state management from a parent component to a child component in order to share data between multiple child components.
Press true if you believe the statement is correct, or false otherwise.
Component Composition
In React, component composition refers to the technique of combining smaller components together to build larger and more complex components.
Think of component composition as a way of building a hierarchy of reusable building blocks. Each component represents a specific functionality or feature, and by composing them together, we can create powerful and flexible UIs.
Let's consider an example to better understand component composition. Suppose we want to build a basketball scoreboard component in React. The scoreboard component will display the scores of two teams: the Lakers and the Raptors.
To implement this, we can create smaller components for each team's score and then compose them inside a parent component called Scoreboard
.
Here's an example implementation:
1import React from 'react';
2
3function Scoreboard() {
4 return (
5 <div>
6 <h2>Scoreboard</h2>
7 <TeamScore teamName="Lakers" score={98} />
8 <TeamScore teamName="Raptors" score={102} />
9 </div>
10 );
11}
12
13function TeamScore({ teamName, score }) {
14 return (
15 <div>
16 <h3>{teamName}</h3>
17 <p>Score: {score}</p>
18 </div>
19 );
20}
21
22export default Scoreboard;
In this example, the Scoreboard
component is the parent component that contains two instances of the TeamScore
component. Each TeamScore
component displays the team name and score.
By composing the TeamScore
components inside the Scoreboard
component, we can build a complete scoreboard UI that renders the scores of both teams.
This technique of component composition allows us to break down complex UIs into smaller and more manageable components. We can reuse these smaller components in multiple places of our application and easily update or modify them as needed.
Component composition is a powerful concept in React that promotes reusability and modular development. It enables us to build complex UIs by combining simple and independent building blocks.
xxxxxxxxxx
// Let's suppose we want to build a simple basketball scoreboard component in React.
import React from 'react';
function Scoreboard() {
return (
<div>
<h2>Scoreboard</h2>
<TeamScore teamName="Lakers" score={98} />
<TeamScore teamName="Raptors" score={102} />
</div>
);
}
function TeamScore({ teamName, score }) {
return (
<div>
<h3>{teamName}</h3>
<p>Score: {score}</p>
</div>
);
}
export default Scoreboard;
Try this exercise. Click the correct answer from the options.
What is component composition?
Click the option that best answers the question.
- Combining smaller components together to build larger and more complex components
- Passing data from parent component to child component using props
- Handling user interactions and events in React components
- Creating functional components using arrow functions
Lifecycle Methods in React
Lifecycle methods are special methods that are automatically called by React at specific points during the life cycle of a component. These methods can be used to perform actions at different stages of the component's life cycle, such as when it is created, updated, or destroyed.
In class components, some common lifecycle methods include:
constructor()
: This method is called when a component is first created. It is used to initialize the component's state and bind event handlers.componentDidMount()
: This method is called after the component has been rendered to the DOM. It is often used to fetch data from an API or set up event listeners.componentDidUpdate(prevProps, prevState)
: This method is called after a component has been updated. It receives the previous props and state as arguments, allowing you to compare the current and previous values and perform any necessary actions.componentWillUnmount()
: This method is called just before a component is removed from the DOM. It is used to clean up any resources or event listeners created in thecomponentDidMount()
method.
Here's an example of a class component with lifecycle methods:
1import React, { Component } from 'react';
2
3class Counter extends Component {
4 constructor(props) {
5 super(props);
6 this.state = {
7 count: 0
8 };
9 }
10
11 componentDidMount() {
12 console.log('Component mounted');
13 }
14
15 componentDidUpdate(prevProps, prevState) {
16 if (prevState.count !== this.state.count) {
17 console.log('Count updated');
18 }
19 }
20
21 componentWillUnmount() {
22 console.log('Component unmounted');
23 }
24
25 incrementCount() {
26 this.setState(prevState => ({ count: prevState.count + 1 }));
27 }
28
29 render() {
30 return (
31 <div>
32 <h2>Counter: {this.state.count}</h2>
33 <button onClick={() => this.incrementCount()}>Increment</button>
34 </div>
35 );
36 }
37}
38
39export default Counter;
xxxxxxxxxx
export default Counter;
// Replace with relevant code or explanation about lifecycle methods
// Make sure to provide examples and explanations specific to class components
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
componentDidMount() {
console.log('Component mounted');
}
componentDidUpdate(prevProps, prevState) {
if (prevState.count !== this.state.count) {
console.log('Count updated');
}
}
componentWillUnmount() {
console.log('Component unmounted');
}
incrementCount() {
this.setState(prevState => ({ count: prevState.count + 1 }));
Try this exercise. Click the correct answer from the options.
Which lifecycle method is called after a component has been updated?
Click the option that best answers the question.
- componentDidMount()
- componentWillUnmount()
- componentDidUpdate()
- constructor()
React Hooks
React Hooks are a new addition in React 16.8 that allow you to use state and other React features without using class components. Hooks are functions that let you 'hook into' React state and lifecycle features from function components.
One of the most commonly used hooks is the useState
hook. It allows you to add state to your functional components. Let's take a look at an example:
1import React, { useState } from 'react';
2
3function Counter() {
4 const [count, setCount] = useState(0);
5
6 function increment() {
7 setCount(prevCount => prevCount + 1);
8 }
9
10 return (
11 <div>
12 <h2>Count: {count}</h2>
13 <button onClick={increment}>Increment</button>
14 </div>
15 );
16}
17
18export default Counter;
In this example, we're using the useState
hook to add a state variable count
and a function setCount
that allows us to update the value of count
. The initial value of count
is set to 0.
Whenever the button is clicked, the increment
function is called which updates the value of count
by incrementing it by 1. The updated value is then displayed in the h2
tag.
React Hooks allow you to easily manage state and perform side effects in functional components, making them a powerful tool for building React applications.
By using hooks, you can write more concise and readable code compared to class components, as you no longer need to deal with the complexities of the this
keyword and lifecycle methods.
xxxxxxxxxx
// React Hooks Example
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function increment() {
setCount(prevCount => prevCount + 1);
}
return (
<div>
<h2>Count: {count}</h2>
<button onClick={increment}>Increment</button>
</div>
);
}
export default Counter;
Try this exercise. Is this statement true or false?
React Hooks allow you to add state and other React features to function components without using class components.
Press true if you believe the statement is correct, or false otherwise.
Generating complete for this lesson!