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
3
votes
2 answers

useState and initial value as array does not work

I'm trying to use react hooks in my project and I have a problem with useState when I use array as value. As you can see in my code below, when I display zoneItems that it should be like items array I get an empty array all the time. Would you have…
Victor
  • 37
  • 1
  • 9
3
votes
3 answers

Too many re-renders in React

The program should take the input user typed, search the data and return results in a drop down list. When the userinput is more than 3 symbols, the Search() is called and I get "Error: Too many re-renders". Can't find where is the render…
Gustė
  • 119
  • 3
  • 12
3
votes
2 answers

How can I access React state in my eventHandler?

This is my state: const [markers, setMarkers] = useState([]) I initialise a Leaflet map in a useEffect hook. It has a click eventHandler. useEffect(() => { map.current = Leaflet.map('mapid').setView([46.378333, 13.836667], 12) . . …
Juuro
  • 1,487
  • 5
  • 16
  • 27
3
votes
3 answers

React useState() array not updating

I have created new custom selectbox using React. There is pre-populated array which I am loading on component load (using useEffect). When user search for any non existing country there would be a option to add. So I used useState hooked. Code is as…
Anand Deep Singh
  • 2,560
  • 3
  • 22
  • 28
3
votes
5 answers

React Hooks - set state to initial state

I am using React useState to create an object in state. This is updated to an object of data after a successful API call. I have a form that can change this state, but I also have a cancel button. How can i restore this state to its initial values…
Paul
  • 219
  • 1
  • 3
  • 11
3
votes
2 answers

React UseState hook, unable to concatenate arrays

I am encountering some strange behavior when attempting to add two arrays together in react. Each array chunk has a length of 50 items. My list never grows beyond 50. I have tried both with spread operator and concat method, with and without wrapper…
psquizzle
  • 175
  • 3
  • 15
3
votes
2 answers

'setState' of useState hook occurs twice by one call

There is a board with squares their value relies on an array, it is handled with useState hook. Every click should raise the value by one, but unfortunately, it raises it by two (except the first click). My questions are: (1) Why is it happen, (2)…
yoni
  • 1,164
  • 2
  • 13
  • 29
3
votes
2 answers

reactJS framework mouseOver and mouseEnter are not recognised, though onClick works fine

My code: import React, {useState} from 'react'; function HeaderNavbar() { console.log("HeaderNavbar: starting"); var [isMouseEnter, setMouseEnter] = React.useState(0); console.log("HeaderNavbar: isMouseEnter set to [" + isMouseEnter +…
SunbeamRapier
  • 127
  • 2
  • 11
3
votes
2 answers

Type Error : destroy is not a function in Reactjs while call api async function in useEffect hook

const [dailyData, setDailyData] = useState([]); useEffect(async () => { const fetchData = await fetchDailyData(); // fetchDailyData() is calling Api setDailyData(fetchData); console.log(fetchData); //fetchData print the value but…
Naveen kumar
  • 33
  • 2
  • 4
3
votes
1 answer

React setState with callback in functional components

I have a very simple example I wrote in a class component: setErrorMessage(msg) { this.setState({error_message: msg}, () => { setTimeout(() => { this.setState({error_message: ''}) }, 5000); }); …
Slowwie
  • 1,146
  • 2
  • 20
  • 36
3
votes
3 answers

Filtering duplicates from two arrays

I am trying to filter duplicates from two arrays... responseData.posts and fbFormattedPosts are both arrays of post objects. Some posts appear in both arrays with the same 'postId'. I'd like to set my state to an array containing all posts of…
Nick McLean
  • 601
  • 1
  • 9
  • 23
3
votes
1 answer

TypeError: Cannot read property 'get' of undefined with useEffect and Axios

I'm trying to fetch some data inside a React hook. Nothing fancy here, just some loading text and then replace once my data loads. But I keep hitting my catch block and get Failed to fetch data: Cannot read property 'get' of undefined TypeError:…
Devon Deason
  • 125
  • 2
  • 8
3
votes
1 answer

React hook useRender called twice if bailing and setting state afterward

I'm not sure if this is expected behavior, but if you bail out of a dispatch (https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-dispatch) while using the useReducer hook, the action occurs twice if it's followed by a render. Let me…
3
votes
1 answer

Iterating files from file input

I am trying to iterate all files from the file input and are following a sample. I get values from the upload control but I cannot iterate it. Is it because it is async, or there is some protection in the browser? I can see the array in the console.…
Thomas Segato
  • 4,567
  • 11
  • 55
  • 104
3
votes
1 answer

How to set the value of inputs with ReactJS hooks?

I want to fill the inputs value of a form with default values once the modal is opened I did it with pure javascript using document.getElementById(textId).value='some value as follow: for(var i=0; i
Ali
  • 1,633
  • 7
  • 35
  • 58