Logo

dev-resources.site

for different kinds of informations.

{useState} HooK { Briefly Explained};

Published at
9/9/2024
Categories
hooks
react
jsx
javascript
Author
marlonmunoz
Categories
4 categories in total
hooks
open
react
open
jsx
open
javascript
open
Author
11 person written this
marlonmunoz
open
{useState} HooK { Briefly Explained};

useState is a React Hook that allows you to add state to your components by returning an array with two variables: state, setState. The current state and the function that becomes the setter function when it is called. It can be used to track data or properties that need to be tracked in an application, such as strings, numbers, booleans, arrays, or objects.
Example:

const [state, setState] = useState();
Enter fullscreen mode Exit fullscreen mode

In simple terms, state will change at some any point and it needs to be updated, therefore 'setState' will update the state, triggering a re-render of your components over time.

In addition, useState can hold any kind of Javascript value, including objects. Something to ALWAYS keep in mind is that you should never change objects that you hold in React state directly. First, you need to create a new one or create a copy of an existing one and then setState to the new copy. For example:

// Objects
const [state, setState] = useState({name: 'Marlo', age: 56});

const updateName = () => {
  setState({...state, name: 'Karlo'});
};

const updateAge = () => {
  setState({...state, age: 96});
};
---------------------------------------------------------------------------------
// Arrays
const [array, setArray] = useState([1, 2, 3, 4, 5]);

const addItem = () => {
  setArray([...array, 6]);
};

const removeItem = () => {
  setArray(array.slice(0, array.length - 1));
};
Enter fullscreen mode Exit fullscreen mode

To use useState in a React component, first you need to import it form React by writing the following code in the top of the component's page in two different ways, both work perfectly so you can choose your poison.

import React from 'react'; 
import {useState} from 'react';
Enter fullscreen mode Exit fullscreen mode

or you can write in one line

import React, {useState} from 'react';
Enter fullscreen mode Exit fullscreen mode

React Hook useState can be called at the top level of a component or within custom hooks but not inside loops or conditions.

const [initialState, setInitialState] = useState();
Enter fullscreen mode Exit fullscreen mode

the initialState is only used during the initial render and will be disregard in subsequent renders.

jsx Article's
30 articles in total
Favicon
Why JSX/TSX Isn't Cool
Favicon
Building Static HTML Pages with JSX Server-Side Rendering
Favicon
Task completed
Favicon
Why React Can Surprise You (And How to Tame It)
Favicon
Render Block component in Next JS and Headless CMS
Favicon
Full Guide For React Developer
Favicon
Imagining React Without JSX: A Developer's Worst Nightmare
Favicon
Projeto de Integração: FastAPI, Jinja2 e JSX
Favicon
Introducing Brisa: Full-stack Web Platform Framework πŸ”₯
Favicon
Understanding JSX in React
Favicon
How React JSX Gets Transformed Into JavaScript Behind the Scenes
Favicon
Day 3: Understanding JSX and Rendering Elements - ReactJS
Favicon
A Comprehensive Guide to Writing JSX in React (with Vite)
Favicon
{useState} HooK { Briefly Explained};
Favicon
Build Reactive Web Components with SSR
Favicon
JSX Limitations and Best Practices in React
Favicon
Making headless components easy to style
Favicon
React HooK= { Briefly Explained};
Favicon
How JSX Works
Favicon
Everything About JSX Syntax And Its Basics: A Quick Guide
Favicon
Converting Extension from JS to JSX
Favicon
Mastering JSX Editing in Emacs with Tree-sitter
Favicon
Building a dynamic Canvas rendering engine using JSX
Favicon
Comprendre les Props en React.js
Favicon
iconSvg
Favicon
An 85 Lines of Code Static Site Generator with Bun and JSX
Favicon
Can use React Smooth Scroll package and React Router Links on the same website?
Favicon
Dynamic background image loading
Favicon
How to render JSX to whatever you want with a custom JSX Renderer
Favicon
CREATE A LIBRARY WITH JSX & CUSTOM STATE

Featured ones: