Questions tagged [use-state]

useState related questions for reactjs hooks api.

Basic React Hook

useState

const [state, setState] = useState(initialState);

Returns a state value, and a function to update it.

Resources

useState hook reference

Common Questions/Issues

  • Why isn't React state update immediate?
  • How to access the latest value?

useState set method not reflecting change immediately

3329 questions
0
votes
2 answers

How to change setState of object in other object in React?

I've created a state of objects in React, but I need to increase an other object into the same state. However, i can't use the setState() property because its causes a re-render (infinite loop). My state: const [testObject, setTestObject] =…
0
votes
2 answers

Why, despite pushing the elements, does my temp array contain no elements in the second useEffect?

I'm attempting to get the subtopics data, so first I have to get the courses in which a user has registered, then for each course I get the chapters, then for each chapter, I get the topics, and finally, I get the subtopics from each topic and push…
Ajit Kumar
  • 21
  • 3
0
votes
2 answers

React Form loses input on reload

im having trouble with a project of mine with the useState hook. This is for a declaration of an event. This component can be opened from a list of all the other declas{ which works fine} and then everything shows however if you reload the page/open…
gmv
  • 199
  • 1
  • 1
  • 11
0
votes
1 answer

How export usestate, calculate, react, 3 files

I have 3 files that make up a component. I am trying to calculate a bmi result which is split into several files. When I click calculate, the page changes. I have tried to find that I am new to React in general, and these are things that are…
GaryNJ
  • 37
  • 5
0
votes
2 answers

How Toggle Works in React

Following is a simple React Toggle functionality. But I have a confusion. In the first click, I understand that the 'setState' function makes the 'state' to 'true' so the content shows. But in the second click 'setState' is already true. Then how…
Ren Jitsm
  • 429
  • 1
  • 4
  • 16
0
votes
1 answer

useState isn't updating

I'm fetching an api and want to change useState when the api is returned. However, it simply isn't updating. Any suggestions? const fictionApi = "http://localhost:3000/fiction" const nonFictionApi = "http://localhost:3000/non_fiction" const…
0
votes
0 answers

How to slice a string when using the useState hook?

I have a component with the following handler: The handler looks like this: const handler = (e: KeyboardEvent) => { if (e.key === 'Backspace') { const slicedString = input.slice(0, -1); …
0
votes
3 answers

Maintaining react state with a hierarchical object using react hooks (add or update)

I have an a state object in React that looks something like this (book/chapter/section/item): const book = { id: "123", name: "book1", chapters: [ { id: "123", name: "chapter1", sections: [ …
MattoMK
  • 609
  • 1
  • 8
  • 25
0
votes
3 answers

How to add if condition inside of useState in React

I want to add if condition inside of use state. So here is my code example: const [buttonName, setButtonName] = useState('Connect Wallet'); const changeButtonName = () => { localStorage.getItem('address') ? setButtonName('Wallet Connected') :…
0
votes
0 answers

React useState Hook not updating state correctly

I am working on a Memory Game Project in React. I have an array of pokemon objects (since I use those as input for the game) and I set the nextPokemon in state to be an object at a random index in said pokemonsArray. Afterwards, I am adding the…
0
votes
1 answer

How can I use more than one setTimeOut in a single react component?

Using useState and setTimeout, I am trying to run one timer for 4 seconds, and a second one for an additional 4 seconds (totaling 8 seconds on the second one). But when I use 2 setTimeOut's in a single react control, I get a strange behavior where…
JackieG
  • 1
  • 2
0
votes
2 answers

React updating array of states via useState

I'm new to react/js and need help. I have the following code: import React, { useState, useEffect } from 'react' function MainBoard() { let letter = '' const [lastLetter, setFirstLetter] = useState('') function…
AoXNeR
  • 47
  • 6
0
votes
2 answers

Pushing array with useState()

Everyone! i just have this kind of problem that i can`t fix. this is my App.js import { useState } from "react" import Header from "./components/header" import FeedbackList from "./components/FeedbackList" import FeedbackData from…
0
votes
1 answer

axios.post is not sent on first attempt. But works in second try in React Native

I am trying to make an API call on changing a state which is triggered by a toggling button in React Native. For this I have used useEffect to trigger API call. const GroundFloorLamps = props => { const [lampsState, setLampsState] = useState({ …
0
votes
1 answer

How can I return two components within the same function in react?

I am working on a react native, and I want to know if there is a way to return two components that belong to the same function. I have the following code: import React, { useState } from "react"; import { Text, View } from "react-native"; const…
Toms_Hd3z
  • 129
  • 1
  • 11
1 2 3
99
100