React useState() hook introduction 🎣

Bro Code
20 Nov 202316:32

Summary

TLDRIn this video, the presenter introduces React Hooks, a feature that enables functional components to use React features without writing class components, introduced in React 16.8. The focus is on the useState hook, which allows for stateful variables and their update functions. The tutorial walks through creating a reactive counter program, demonstrating how to use useState to manage state changes that trigger re-renders in the virtual DOM. The video also covers creating stateful variables for name, age, and Boolean values, and concludes with styling the counter with CSS.

Takeaways

  • πŸ“Œ React hooks were introduced in version 16.8, allowing functional components to use state and other React features without writing class components.
  • πŸ”‘ The `useState` hook is fundamental for creating stateful variables and their setter functions within functional components.
  • πŸ’‘ Using `useState` provides a way to update state and trigger re-renders in the React virtual DOM, unlike regular variables which do not cause re-renders.
  • πŸ› οΈ The `useState` hook returns an array with two elements: the current state value and a function to update it.
  • πŸ“ When using `useState`, you can optionally pass an initial state value; if not provided, it defaults to `null`.
  • πŸ”„ The script demonstrates creating a counter program using `useState` to manage incrementing, decrementing, and resetting a count.
  • 🎨 Conditional rendering is shown by toggling a Boolean state to display 'yes' or 'no' based on its value.
  • πŸ‘©β€πŸ’» The video includes a step-by-step guide on setting up a new React component and using hooks within it.
  • 🌐 The script covers the necessity of importing React and destructuring `useState` from it to use within functional components.
  • πŸ–ŒοΈ CSS styling is applied to enhance the visual presentation of the counter component, demonstrating the integration of React functionality with styling.

Q & A

  • What is a React Hook?

    -A React Hook is a special function that allows functional components to use React features without writing class components. It was introduced in React version 16.8.

  • Why were React Hooks introduced?

    -React Hooks were introduced to allow developers to use state and other React features in functional components without the need to write class components.

  • What is the most widely used React Hook?

    -The most widely used React Hook is `useState`, which allows the creation of a stateful variable and a setter function to update its value.

  • How does the `useState` hook work?

    -The `useState` hook returns an array with two elements: a stateful variable and a setter function. The setter function is used to update the state, which triggers a re-render of the component.

  • Can you provide an example of how to use the `useState` hook?

    -To use `useState`, you import it from React and call it within a functional component. For example, `const [name, setName] = useState('');` creates a stateful variable `name` and a setter function `setName`.

  • What is the purpose of the setter function returned by `useState`?

    -The setter function returned by `useState` is used to update the stateful variable. When called with a new value, it updates the state and causes the component to re-render with the new state.

  • How can you set an initial state with `useState`?

    -You can set an initial state by passing it as an argument to the `useState` hook. For example, `const [age, setAge] = useState(0);` sets the initial state of `age` to 0.

  • What is the difference between a normal variable and a stateful variable in React?

    -A normal variable does not trigger a re-render when updated, whereas a stateful variable created with `useState` does trigger a re-render, allowing the UI to reflect the updated state.

  • Can you explain the concept of conditional rendering in the context of the `useState` hook?

    -Conditional rendering with `useState` can be achieved using logical operators to determine what to display based on the state. For example, `isEmployed ? 'Yes' : 'No'` will display 'Yes' if `isEmployed` is true, and 'No' otherwise.

  • How do you create a counter program using React Hooks?

    -To create a counter program, you use the `useState` hook to manage the count state and provide functions to increment, decrement, and reset the count. These functions are then used as event handlers for buttons in the component's JSX.

  • What are some other React Hooks mentioned in the script?

    -Other React Hooks mentioned in the script include `useEffect`, `useContext`, `useReducer`, and `useCallback`, each serving different purposes such as managing side effects, providing context, and optimizing performance.

Outlines

00:00

πŸ› οΈ Introduction to React Hooks

The paragraph introduces the concept of React Hooks, explaining that they are special functions allowing functional components to utilize React features without writing class components. This was introduced in React version 16.8. The paragraph also discusses the 'useState' hook, which is the most commonly used. It allows for the creation of a stateful variable and a setter function to update its value. The video aims to create a reactive counter program using the useState hook by the end.

05:01

🎯 Creating a Stateful Variable with useState

This section provides a step-by-step guide on using the 'useState' hook to create a stateful variable and a setter function in a functional component. It explains the process of importing the hook, using array destructuring to extract the stateful variable and setter function, and updating the stateful variable with the setter function. The paragraph also includes a practical example where a 'name' variable is updated using a button click, demonstrating the re-rendering of the virtual DOM when the stateful variable is changed.

10:01

πŸ”’ Incrementing and Toggling State with useState

The paragraph delves into creating additional stateful variables such as 'age' and a Boolean 'isEmployed'. It explains how to increment the 'age' variable by one or more with each button click and toggle the 'isEmployed' Boolean value between true and false. The video script includes the code for creating these variables, updating them, and rendering the updated values in the UI. It also touches on conditional rendering using the ternary operator for displaying the 'isEmployed' status.

15:04

πŸ”„ Building a Counter Program with useState

The final paragraph focuses on building a counter program using the 'useState' hook. It outlines the creation of a 'count' stateful variable with initial value zero and functions to increment, decrement, and reset the counter. The paragraph includes the JSX code for rendering the counter, buttons for controlling the counter, and a brief mention of styling the counter component with CSS. The video script concludes with a demonstration of the counter functionality and a summary of the 'useState' hook's capabilities.

Mindmap

Keywords

πŸ’‘React Hooks

React Hooks are functions that allow you to 'hook into' React state and lifecycle features from functional components. In the video, the presenter explains that hooks were introduced in React version 16.8, enabling developers to use React features without writing class components. Hooks like `useState` and `useEffect` are mentioned as examples, which are used to manage state and side effects in function-based components respectively.

πŸ’‘useState

The `useState` hook is a fundamental React Hook that lets you add state to functional components. The video script describes `useState` as a way to create a stateful variable and a setter function to update its value. This hook is crucial for building interactive UIs, as it allows for the dynamic updating of the component's state, which in turn updates the virtual DOM.

πŸ’‘Functional Components

Functional components in React are functions that return a React element. The video emphasizes the shift from class components to functional components facilitated by hooks. Before hooks, stateful logic was typically managed in class components, but now functional components can handle state and lifecycle through hooks, making them more concise and easier to manage.

πŸ’‘Class Components

Class components are a type of component in React that use ES6 class syntax. The video script contrasts class components with functional components, noting that prior to hooks, state management and lifecycle features were only available in class components. Hooks have since made it unnecessary to write class components for stateful logic in many cases.

πŸ’‘Virtual DOM

The Virtual DOM (Document Object Model) is a programming concept where an in-memory representation of the DOM is kept and updated, minimizing actual DOM manipulations for performance. The script explains that when the state is updated using the `useState` hook, the change is reflected in the virtual DOM, which then triggers a re-render of the component.

πŸ’‘Array Destructuring

Array destructuring is a JavaScript feature that allows for the extraction of values from arrays. In the context of the video, array destructuring is used to extract the state variable and its updater function from the array returned by the `useState` hook. This makes it easier to work with the state and its updater function as separate variables.

πŸ’‘Stateful Variable

A stateful variable is a variable that holds a value that can change over time. The video script uses the term to describe the variable created by the `useState` hook. Unlike normal variables, stateful variables in React are part of the component's state, and their updates can trigger re-renders of the component.

πŸ’‘Setter Function

A setter function is a function that sets or updates the value of a variable. In the video, the presenter explains that the `useState` hook returns an array containing a stateful variable and its corresponding setter function. This setter function is used to update the state in a way that React understands and can track, leading to UI updates.

πŸ’‘Conditional Rendering

Conditional rendering in React is the process of rendering different components or elements based on certain conditions. The video script demonstrates conditional rendering with a Boolean state variable, where the UI displays 'yes' or 'no' based on whether the variable is true or false. This showcases how React can dynamically change what is displayed on the screen based on state.

πŸ’‘CSS Styling

CSS styling is used to define the appearance of web elements. In the video, after creating a functional counter component, the presenter applies CSS to style the component. This includes setting font sizes, colors, margins, and other properties to make the counter visually appealing and interactive.

Highlights

Introduction to the useState React hook and its role in functional components.

Explanation of React hooks as a feature introduced in React version 16.8.

Overview of various React hooks including useState, useEffect, useContext, useReducer, useCallback, and more.

Detailed explanation of useState hook for creating stateful variables and setter functions.

Demonstration of how to use useState hook in a functional component.

Importing the useState hook from the React library using object destructuring.

Creating a stateful variable and setter function using array destructuring.

Example of updating the stateful variable using its setter function to trigger a rerender.

Explanation of the difference between stateful variables and normal variables in React.

Creating an initial state for the useState hook.

Incrementing a stateful variable using a function and the setter.

Creating a Boolean stateful variable and toggling its value with a button click.

Conditional rendering based on the stateful variable's value using the ternary operator.

Building a counter program using the useState hook to manage count state.

Creating functions to increment, decrement, and reset the counter state.

Styling the counter program with CSS for better user interface.

Final demonstration of the fully functional counter program with styled buttons and display.

Summary of the useState hook's capabilities and its importance in React functional components.

Transcripts

play00:00

hey what's going on everybody in today's

play00:01

video I'm going to explain the usate

play00:03

react hook and near the end of this

play00:05

video we're going to create a reactive

play00:07

counter program so sit back relax and

play00:10

enjoy the show I haven't explained what

play00:12

react hooks are yet a react Hook is a

play00:15

special function that allows functional

play00:17

components to use react features without

play00:19

writing class components this was a

play00:21

change made in react version 16.8

play00:24

basically we no longer need to write

play00:26

class components we can write function

play00:28

based components that use rea hooks to

play00:30

use react features there are many react

play00:33

hooks if a function begins with use it's

play00:36

probably a react hook use State use

play00:38

effect use context use reducer use

play00:40

callback and more use state is the most

play00:42

widely used use state is a react hook

play00:45

that allows the creation of a stateful

play00:47

variable and a Setter function to update

play00:50

its value in the virtual Dom basically

play00:53

by using the UST State hook we can

play00:55

create not just a variable but a

play00:57

stateful variable when you update this

play00:59

variable that change will be reflected

play01:01

in the virtual Dom normal variables

play01:03

don't when you use the UST State hook

play01:06

you're given a variable and a Setter

play01:07

function specifically for that variable

play01:10

so what we'll do in this example is

play01:11

create a new component going to our

play01:13

source folder we're going to create a

play01:15

new file we'll name this component my

play01:19

component we use react hooks in function

play01:22

based components so make sure you're not

play01:24

writing a class

play01:26

component with this component we will

play01:29

export it export default my component

play01:34

then be sure to import this because I'm

play01:35

probably going to forget if we don't do

play01:37

it now so we will import my

play01:41

component from its location

play01:45

do/ my component and this is a jsx file

play01:50

let's return one my

play01:54

component and we are ready to begin in

play01:57

order to use a react hook we need to

play01:59

import it it at the top of this

play02:01

component we will import the react

play02:04

Library however we don't need the entire

play02:07

react Library we can use object

play02:09

destructuring to extract individual

play02:12

variables or functions I would just like

play02:14

the UST State function we don't need the

play02:17

entire react library from its location

play02:21

react we now have access to this use

play02:24

State function using the use State

play02:26

function we'll create a stateful

play02:28

variable and a set function to update

play02:31

that variable so let's declare const

play02:34

we're going to use a set of straight

play02:35

brackets for array

play02:37

destructuring equals use State function

play02:43

the use State function returns an array

play02:45

with two elements a variable and a

play02:48

Setter function we're going to use array

play02:50

destructuring to destructure these two

play02:52

elements we'll create a stateful

play02:54

variable for name then we're given a

play02:57

Setter function specifically for this

play02:59

variable a common naming convention is

play03:01

to typee set then the variable name with

play03:04

camel case naming convention and that's

play03:07

it if we ever need to change the value

play03:09

of the stateful variable we have to do

play03:11

so with this Setter it's a function at

play03:14

the bottom we're going to

play03:16

return a

play03:20

development within this development

play03:22

we'll create a paragraph element and a

play03:25

button we'll begin with a paragraph

play03:27

element that has text of name

play03:30

and I'm going to zoom in a little bit I

play03:32

will insert some JavaScript using curly

play03:35

braces let's display our name following

play03:38

our paragraph element let's include a

play03:41

button the button will have an onclick

play03:44

attribute equal to a JavaScript function

play03:47

so we need a set of curly braces to

play03:49

embed that let's create a function to

play03:53

update name for the text of the button

play03:57

let's say set name

play04:00

name all right now we just need to

play04:02

declare this function I'll use an arrow

play04:05

function const update name equals Arrow

play04:12

function so what would we like to do

play04:15

okay let's attempt to set our name equal

play04:18

to type your name or some other name I'm

play04:21

just going to type in

play04:23

SpongeBob if I click on this button we

play04:26

should update our name right well that

play04:29

doesn't appear

play04:30

work so if I were to

play04:33

console.log my name

play04:36

variable then attempt to update it using

play04:39

this button if I were to go to my

play04:42

console hold on I'm going to use let so

play04:45

we can update it if I attempt to change

play04:48

this name of the

play04:49

variable it does so within our

play04:52

console but it doesn't update in react

play04:55

the virtual Dom is still displaying the

play04:57

previous state so if I would like to

play04:59

display display any changes I will want

play05:01

to use that Setter instead of setting

play05:03

our name equal to a new value I'm going

play05:06

to change this back to be a

play05:08

constant we will use the setter function

play05:11

and pass in a new value so let me type

play05:14

in a new

play05:16

value and that should work we have

play05:18

updated our name when you use the setter

play05:21

function of a stateful variable it will

play05:23

trigger a rerender of the virtual Dom

play05:26

normal variables don't that's why the US

play05:29

state Hook is useful we can create a

play05:31

stateful variable when the stateful

play05:33

variable gets updated with its Setter

play05:35

function it triggers a render of the

play05:37

virtual Dom with the UST State function

play05:40

you can pass in an initial state

play05:43

currently we're not passing in anything

play05:44

for the initial State I will set this to

play05:46

be

play05:47

guested when I refresh everything and

play05:50

start over the initial state is guessed

play05:53

whatever value you pass in to the UST

play05:55

State hook then I can set my name again

play05:57

to something else now we're going going

play05:59

to create an age variable and increment

play06:02

it const we're going to use array to

play06:05

structuring we need a stateful variable

play06:08

like age and a Setter function for that

play06:11

age set age equals use

play06:17

state if you would like an initial value

play06:19

you can place that within the UST State

play06:21

function I'll set the initial value of

play06:23

age to be

play06:26

zero I'm going to copy this paragraph

play06:28

and this button

play06:31

change name to be

play06:34

age we'll create a function to increment

play06:39

age the text on the button will

play06:42

be increment

play06:44

age now we just need this

play06:47

function

play06:49

const increment age I'll use an arrow

play06:55

function to increment our age we will

play06:58

use the set age

play07:01

function let's take our age +

play07:05

one so our initial value for our age is

play07:09

zero but every time I click on the

play07:10

button we will update the value of that

play07:13

variable so every time I click the

play07:15

button we're increasing our age by

play07:18

one or even a different number this time

play07:21

I'll increase our age by two on each

play07:22

button click so we start at zero then

play07:25

we'll increment by two 2 4 6 8 10

play07:29

now this time we'll create a Boolean

play07:31

variable and toggle it between being

play07:34

true and false using the usate hook we

play07:37

will create const is

play07:41

employed the setter function will be set

play07:45

is

play07:47

employed equals the use State

play07:51

hook I would like an initial value of

play07:55

false let's create a paragraph element

play07:58

and a button

play08:01

I will set the text of the paragraph to

play08:03

be is

play08:06

employed instead of displaying a Boolean

play08:09

directly let's use the tary operator for

play08:12

conditional rendering is employed if

play08:15

that is true we'll display yes otherwise

play08:20

no when we click on the button let's

play08:22

create a function

play08:24

to

play08:25

toggle toggle employed

play08:30

status that's kind of a Long Function

play08:32

name all right let's create a function

play08:35

const toggle employed status equals an

play08:40

arrow

play08:41

function we will use the setter function

play08:44

set is employed pass in a new value

play08:48

let's switch this value from being false

play08:50

to true and true to false every time we

play08:51

click the button since this is a Boolean

play08:54

we can use the not logical operator to

play08:56

reverse it so let's say not is

play09:01

employed and let's see if this works is

play09:04

employed no oh let's change the text on

play09:07

the button

play09:08

too toggle

play09:12

status there we go all right when we

play09:15

click on the button we can toggle this

play09:17

Boolean from being true to false and

play09:19

false to true and this should happen

play09:21

every time I click the

play09:23

button as a project what we're going to

play09:26

do now is create a counter program so

play09:28

let's close out of my

play09:30

component we'll create a new component

play09:33

for a counter component counter.

play09:37

jsx this will be a function base

play09:40

component

play09:42

function

play09:44

counter then be sure to export it export

play09:48

default

play09:51

counter going back to our app component

play09:53

we will import our counter component

play09:56

from its

play09:58

location counter. jsx then we will

play10:01

include one counter component and that's

play10:05

it in order for us to use the use State

play10:07

hook we have to import it from the react

play10:10

Library import react we'll use object

play10:13

destructuring just to get the US state

play10:16

hook and nothing else from its location

play10:20

of

play10:21

react all we need is one variable a

play10:24

counter let's say const we'll use array

play10:28

destructuring create a stateful variable

play10:31

of count and a Setter function for that

play10:33

count set count equals the use State

play10:39

hook would we like an initial value for

play10:42

count we would like the initial value to

play10:44

be

play10:45

zero we'll create a few functions to

play10:47

increment decrement and reset the

play10:50

counter const

play10:53

increment equals I'll use an arrow

play10:57

function to up update the count to

play11:00

increment it we will use the set count

play11:03

function the value we pass in is Count +

play11:08

one then let's do this for

play11:12

decrement const decrement count minus

play11:17

one then

play11:20

reset const reset for set count we'll

play11:25

pass in zero to reset the

play11:27

count now we're going to return some

play11:31

elements we'll also style this with CSS

play11:34

let's begin with the div

play11:36

element I'm going scroll down a little

play11:42

bit my div element will have a class

play11:45

name equal to

play11:48

counter-

play11:51

container I will create a paragraph

play11:53

element with a class

play11:56

name equal to count

play11:59

display to display the

play12:01

number for the text of the paragraph I

play12:04

will insert some JavaScript and display

play12:06

our count

play12:09

variable we'll create three buttons for

play12:12

the first we'll create a button element

play12:14

this button will have a class

play12:17

name of

play12:19

counter-

play12:21

button the onclick attribute will be set

play12:25

equal to a JavaScript function this

play12:28

first button will be the decrement

play12:29

button for the unclick attribute we will

play12:32

set this equal to the decrement function

play12:35

for the text on the button we'll say

play12:40

decrement and there's our first button

play12:43

so let's copy this button paste it the

play12:47

second button will be for reset the text

play12:50

will be

play12:52

reset then the third button will be

play12:55

increment onclick will be the increment

play12:58

function

play12:59

the text will be

play13:01

increment all right and that's all that

play13:04

we need let's check it for functionality

play13:05

to be sure that everything works we can

play13:08

increment this number we can decrement

play13:11

it and we can reset

play13:13

it so for the icing on the cake let's

play13:16

style it with

play13:18

CSS going to our index CSS stylesheet

play13:22

we'll apply the following CSS let's

play13:24

select our counter

play13:27

container do counter container I will

play13:31

text align

play13:35

Center change the font

play13:38

family I will pick a s serif font of

play13:41

Ariel with a backup of s

play13:45

serif next we'll select the count

play13:49

display select the class of count

play13:51

display that would be this number will

play13:54

increase the font size to something

play13:56

massive like 10 em RM works too okay

play14:00

that's a little too big but I am zoomed

play14:02

in actually you know what that's perfect

play14:05

I will set the margin top to be zero to

play14:09

close this Gap and I will set the margin

play14:13

on the bottom to be 50

play14:16

pixels then let's work on the counter

play14:18

buttons

play14:20

next select the class of counter button

play14:24

I will set the width to be 150 pixels

play14:31

the height to be 50

play14:35

pixels the font

play14:37

size to be 1.5

play14:42

em I will set the font weight to be

play14:47

bold set the margin to

play14:49

be 0 pixels by 5

play14:53

pixels this would be for the left and

play14:55

right of the

play14:56

buttons change the background

play15:00

color pick a color that you

play15:03

like I'm going to use hsl values

play15:08

though that's pretty good I will set the

play15:12

color of the font to be

play15:15

white remove the

play15:17

border border

play15:20

none set the Border radius to around the

play15:24

corners five

play15:27

pixels

play15:29

and change our cursor to be a pointer if

play15:32

we hover over the button when we hover

play15:35

over the button I'm going to change the

play15:36

background color of the button so with

play15:39

the counter button class with the

play15:41

counter button class we will select the

play15:43

hover sudo class we'll take our

play15:46

background color decrease the lightness

play15:49

to be 10%

play15:51

darker there we

play15:52

are and that is all the CSS styling that

play15:55

we need we have our counter program we

play15:57

can increment the counter we can

play15:59

decrement it and we can reset

play16:02

it all right everybody so that is the

play16:05

use State react hook it allows the

play16:07

creation of a stateful variable and a

play16:10

Setter function to update its value in

play16:12

the virtual Dom when you include the UST

play16:15

State hook you're given an array of two

play16:17

elements we use array destructuring to

play16:20

create a stateful variable and a Setter

play16:22

function to update that variable and

play16:24

well everybody that is the UST State

play16:26

hook in react

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
React HooksuseStateFunctional ComponentsCoding TutorialWeb DevelopmentJavaScriptState ManagementUI UpdatesInteractive App