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
1 answer

When to use setState hook in React

I have a component where I load a list of products with their information. useEffect(() => { getProducts().then((products) => { if (products) { setLoaded(true); setProducts([...products]); } }); }, []); Then…
0
votes
2 answers

useState hook not "reacting" when state variable modified

I have created a React application to test out the useState hook. This the variable of concern: let [blocks, setBlocks] = useState([ {text: "Hello", id: 1}, {text: "This is google", id: 2}, {text: "Wassup", id: 3}, {text: "Last…
0
votes
0 answers

Conditional rendering in for respective row when a button in that row is clicked

I want to implement a functionality where I can render the values of array and when I click on update for a particular record, conditional rendering should happen for some td's. I have implemented this logic but it's not working as expected. const…
0
votes
1 answer

setState array is not accepting the value of another array and giving empty data

i am trying to get the images url from the html file input to send them to the backend and upload to cloudinary but the problem is i am having the imagesArr but when i set the images state it isnt working and giving me an empty array i also tried…
0
votes
1 answer

react redux dispatches for infinite time with useEffect

I am really having a hard time with this, if anyone could help, i would really appreciate. So, what I am trying to do is, after the listFeatured() is fetched, set the home(setHome) its one of the value. But instead it dispatches for unlimited time.…
NroGamerz
  • 189
  • 2
  • 8
0
votes
1 answer

how to set state from loop in react?

i'm trying to change img src on hover, so i decided to change src through state by looping through the sourecs array onMouseenter but every time i get the last element only. i read some articles and i knew that i cant't use useState inside a loop,…
mostafa elbanna
  • 195
  • 2
  • 3
  • 9
0
votes
3 answers

React TypeScript select field does not display

I got useState with a list of strings (currencies symbols like "USD", "EUR" etc.) const [symbols, setSymbols] = useState() And I need to display it into the select field, right now I'm using this: const renderInputPanel = () => { …
0
votes
3 answers

Remove element from useState array by index

SOLUTION: Update the key value for the input element to refresh the default value => content of the input element. Deleting an element from the array DID work. Thanks for your help! src:…
ahaMfM
  • 126
  • 11
0
votes
0 answers

How to add values to array using useState?

I am using add to cart function and I am successfully getting the item in my cart on the first click. It just won't increment when I add multiple objects in the cart. It is just displaying one item on a click. The length and the qty of the array…
Harshil Gambhir
  • 337
  • 1
  • 2
  • 8
0
votes
2 answers

ReactJS counter or timer with Start or Stop Button

Need help to understand in useEffect, if I don't put counter and timerCheck in useEffect dependency then what it will effect here. And if I put timerCheck dependency in useEffect then counter increasing 100 time faster Also how can i run this code…
Amit Sharma
  • 31
  • 1
  • 12
0
votes
1 answer

I want to remove the counter when the value reaches to 0 and show the add button again

I have created an add button, when clicked it hides and shows a counter to add to cart. But when the counter is made to go zero. I want to show the add button again and hide the counter. How is it possible to do so? const Card = (product) => { …
Harshil Gambhir
  • 337
  • 1
  • 2
  • 8
0
votes
1 answer

is it good in react to put a variable as a state value and update it directly

now I'm working with react form validation logic I saw some tutorials on the internet about validating a form in react and they made a normal variable and put it as a value to a state and update it directly without calling the "set" function…
0
votes
1 answer

useState does not get updated value

I am working with a list with useState, so I select an element from the list and update the selected record in the database and retrieve the updated list but when going through the list to obtain the updated value(1), the list does not have the…
Ronald
  • 291
  • 3
  • 7
  • 23
0
votes
2 answers

NextJS useState Array not properly setting the state

I'm trying to save an array to a useState array but the state is not updating properly. When the code below is run, the variable first will return the values properly but the variable "data" returns an empty array. When I add console.log("restart")…
o_o
  • 25
  • 3
0
votes
1 answer

How to render different dropdown list dynamically based on selected option in first dropdown in React Js

I don't know where I am making mistake but I have tried all related videos and article but still not able to render dropdown list dynamically based on selected option in first dropdown. there are two dropdown first one has some option so I want to…