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
4
votes
6 answers

React Native Hooks - How use useStates in Styles

I am using hooks to write a react native app. I have problem with using states inside Styles. The background of text container is red, by default and after pressing the Confirm button should be changed to green. At the moment I face error when I…
Janet Pedram
  • 51
  • 1
  • 3
4
votes
1 answer

How to pass id into navlink in react JavaScript

I am fetching details from database and displaying on page and I want to create an edit button which after click can open that details in editable form. In my case that editable form is (EMPLOYEEFORM). Can you please suggest how to pass id into edit…
Raushan Singh
  • 95
  • 5
  • 16
3
votes
2 answers

setState is changed but the variable assigned to it can not get the updated value

developing a chat app with a loader to wait until receiving the response. According to console.log in the app the isLoading state changes correctly however inside the receiver's object the isLoaded key is not gonna getting update and only show the…
3
votes
2 answers

How to filter array data in React js and update the data to useState

i really need help because i still learning about react js and data structure, i want ask about how to filtering spesific array data from redux and push to useState. dataPortfo: [ { id: 0, title: "Retne Cms", link: "www.google.com", image:…
irahama
  • 65
  • 6
3
votes
2 answers

Why does useState not increment

I'm trying to implement useState to store an incrementing value. When I try incrementing the value it doesn't update it. I have tried manually setting the value to a specific number and this does work. What is wrong? const [cardsIndex,…
chackerian
  • 1,301
  • 2
  • 15
  • 29
3
votes
3 answers

useeffect infinite loop even though state data is not changing

My program goes into an infinite loop constantly calling useEffect() everytime I start the app. I have one state that I don't think is changing other than in the retrieveItemStatus() function so I'm confused on why its going into a loop like it…
3
votes
1 answer

Can we use useState() at multiple time or is there any limit to use it in React Js?

Can we use useState() at multiple time or is there any limit to use it in single component in React Js.
3
votes
1 answer

react-animarker highlight not updating with state change

I have some text that I want to simply highlight using a simple react package called 'react-animarker'. I have tried to use useState hook as I want to dynamically update the speed of the animation as shown. However, the duration does not change the…
rj05
  • 33
  • 5
3
votes
1 answer

Can't setState inside useEffect with "useIsFocused()" from react-navigation as dependency

I use the useIsFocused() function from react-navigation/native as a dependency of useEffect to listen to screen focus, so when I navigate back it enters the if with the isFocused and the myRoute.params.isChecked to execute the function. But it's not…
3
votes
0 answers

useRef's reference value change on re-render caused by strict mode while useState does not

I was trying to run something in useEffect, but did not wanted it to run on initial render so I created a hook useIsMounted. import { useRef, useEffect } from 'react'; export const useIsMounted = () => { const isMountedRef = useRef(false); …
rahul
  • 31
  • 2
3
votes
2 answers

Trying to animate markers movement with react-google-maps smoothly

I'm trying to animate my google maps markers like this... Example of markers animated movement And i tried to replicate this code into react (using, @react-google-maps/api), but couldn't make it work, i'm having multiple problems, number one, would…
Diego
  • 421
  • 3
  • 9
  • 34
3
votes
2 answers

ReactJS useState hook: Can I update array of objects using the state variable itself?

I have: const [list, setList] = useState([]); Somewhere in the component, I want to do: list[index][someKey] = some_updated_value; setList(list); Is this valid? Or, is it better to do: const newList = list; newList[index][someKey] =…
Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71
3
votes
3 answers

update the array of object using useState hook

I am working on next js project and I have the following input object array set to the filesize useState. Here the keys can be considered as the indices of file list and the value is the size of that particular file Let's say 2nd index key-value…
3
votes
2 answers

What is the difference between [...arr, addThisElement], and arr.push(addThisElement) when using useState in ReactJS?

Could someone please explain the difference between this: const arr = users; arr.push({ firstName, email }) setUsers((prevState) => arr) and this: setUsers(prevState => [...prevState, { firstName, email }])
sharif_17
  • 45
  • 3
3
votes
2 answers

How can I update a state variable from a promise?

I am trying to determine if a customer has an active subscription or not. To do this I am utilizing the following code: const stripe = require('stripe')('some-api-key'); export default function Example(){ // the user will automatically be…