Search Header Logo
An introduction to ReactJS State

An introduction to ReactJS State

Assessment

Presentation

Computers

University

Practice Problem

Medium

Created by

Zh L

Used 12+ times

FREE Resource

13 Slides • 7 Questions

1

An
Introduction to ReactJS State

media

Understanding ReactJS State

2

What are React State?

Using React state is like giving your components memory. It allows your components to remember things and reacts to it.

3

What are React State?

  • Components frequently need to respond to user interactions by updating/re-rendering what's displayed on the screen.

  • For example, clicking "next" in an image gallery should switch the displayed image, and clicking "buy" should add an item to the shopping cart.

  • Components require a way to "remember" things, such as the displayed image, or shopping cart contents referred to as "state."

4

useState Hook

To add a state variable, we can use the React Hook: useState

media

5

Anatomy of useState Hook

  1. To add a state variable, import useState from React:
    import { useState } from 'react';

  2. useState returns an array with two elements: [state, setState]:

    1. state: The current value of the state variable. It's where you can store data that you want to keep track of within your component

    2. setState: A function provided by React that allows you to update the state variable.

6

Scenario: Button on Click

Imagine you are building a simple counter application using React.

You want to display a number on the screen, and users can increase or decrease this number by clicking buttons.

7

Step 1) Create a React component called Counter

import React, { useState } from 'react';
function Counter() {

return (

<div>

<h1>Counter App</h1>

<p>Count: 1</p>

<button onClick=add>+</button>

<button onClick=subtract>-</button>

</div>

);

}

  • In this example, we import the useState hook from react.

  • In our Counter component, we return a Count and 2 button which invokes the functions: add and subtract.

8

Step 2) Initializing State

import React, { useState } from 'react';

function Counter() {

const [count, setCount] = useState(0);

return (

<div>

<h1>Counter App</h1>

<p>Count: 1</p>

<button onClick={add}>+</button>

<button onClick={subtract}>-</button>

</div>

);

}

  • Use the useState hook to create a count state variable with an initial value of 0.

  • Define a setCount function used to update this state variable.

9

Step 3) Using the count state variable

import React, { useState } from 'react';

function Counter() {

const [count, setCount] = useState(0);

return (

<div>

<h1>Counter App</h1>

<p>Count: {count}</p>

<button onClick={add}>+</button>

<button onClick={subtract}>-</button>

</div>

);

}

  • Use the count state variable for displaying within the Counter component.

10

Step 4) Using the setCount function to update count

import React, { useState } from 'react';

function Counter() {

const [count, setCount] = useState(0);

return (

<div>

<h1>Counter App</h1>

<p>Count: {count}</p>

<button onClick={() => setCount(count + 1)}>+</button>

<button onClick={() => setCount(count - 1)}>-</button>

</div>

);

}

  • Use the setCount function to update the count state variable

11

Multiple state variables

  • You can have as many state variables of as many types as you like in one component.

  • It is a good idea to have multiple state variables if their state is unrelated. But if you find that you often change two state variables together, it might be easier to combine them into one that holds an object instead

12

Multiple state variables example

function UserProfile() {

// Define an object state variable with initial values

const [user, setUser] = useState({

name: 'John Doe',

email: 'johndoe@example.com',

age: 30,

});
return (
<div>
<p>Name: {user.name}</p>
<p>Email: {user.email}</p>
<p>Age: {user.age}</p>
<button onClick={increaseAge}>Increase Age</button> </div>
);
}

13

Multiple Choice

What is React state used for?

1

To manage external data sources.

2

To manage global application data.

3

To handle routing in React applications.

4

To store and manage component-specific data that can change over time.

14

Multiple Choice

Which hook is commonly used for managing state in functional components in React?

1

useEffect

2

useState

3

useContext

4

useRef

15

Multiple Choice

In a React component, how do you access and modify state?

1

Using the state property and setState method.

2

By directly modifying the state variable.

3

State cannot be accessed or modified in class components.

4

Using the props property.

16

Multiple Choice

What is the purpose of the setState function in React?

1

To retrieve the current state.

2

To delete the state.

3

To set a new state value.

4

To create a state variable.

17

Multiple Choice

How do you initialize a state variable called count with an initial value of 0?

1

const [count, setCount] = useState(0);

2

const count = 0;

3

const count = useState(0);

4

const count = setState(0);

const setCount = setState(0);

18

Multiple Choice

In React, how can you pass state from a parent component to a child component?

1

By defining a state variable with the same name in both components.

2

By directly accessing the parent component's state.

3

State cannot be passed from parent to child components.

4

By passing it as a prop to the child component

19

Multiple Choice

What's the correct way to invoke the updater function to increment the count by 1 when a button is clicked?

1

setCount(prevCount => {

return prevCount + 1;

})

2

const incrementCount = () => {

setCount(count + 1);

};

3

count => {

return setCount(count + 1);

}

4

updateCount => setCount(updateCount + 1);

20

Summary

In this lesson, you have learnt:

  • What are React State?

  • What is the purpose of using State?

  • The anatomy of a useState Hook

  • How to initialize a state

  • What are the purpose of the state variable and updater function?

  • How to initialize multiple state variables?

An
Introduction to ReactJS State

media

Understanding ReactJS State

Show answer

Auto Play

Slide 1 / 20

SLIDE