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

React hooks, declaring multiple state values from prop

I'm pretty new to React and React hooks in general, I'm building a react app for my final project and I wanted to make some component (Advanced search in this example) as generalized as possible which means I want to pass "dataFields" and the…
yoni hodeffi
  • 33
  • 1
  • 3
3
votes
3 answers

useState variable not latest version in delete function, React

I have a list of div's that contain question fields. For every button click i add a new line of question fields and memorize in state the whole list and how many lines there are. I tried adding a delete button but when my delete functions it seems…
PieterjanV
  • 31
  • 1
2
votes
2 answers

Merge two arrays using use state hook in React

First of all, I use React.js in my project. I want to merge two arrays using use state hook. I have two arrays. The first array is [ { "id": 1, "count": "2" }, { "id": 2, "count": "1" } { "id": 3, "count": "1" } …
2
votes
2 answers

useState doesn't update state passed in component

The SelectedColumn value doesn't come in the CustomHeader component. However, setSelectedColumn works! Why ? Also, I'm passing CustomHeader to constant components that use useMemo. Without useMemo CustomHeader doesn't work. const [selectedColumn,…
2
votes
0 answers

I want to use search a particular data from all the available data to show its all value in reacts using search() and filter()

I want to show only one data using search() and filter() but its showing error in runtime that it's not a function below is my code please help me out. By the way I am new to reactJS so I don't know much about it. Thank you. Here I have created the…
2
votes
1 answer

How to map through data coming from smart contract

So I'm reading data from a smart contract and trying to map through it to display on a frontend but it keeps returning an error data is undefined I'll post my code below, I'm really looking forward to some help import React , { useState , useEffect…
mxnuchim
  • 35
  • 6
2
votes
1 answer

How to make a single submit button for a quiz app instead of making each individual question mark itself?

I have succeeded in making a quiz app that shows red if the wrong option is clicked and green if the right one is clicked. The thing is that since I am getting the options from an api, I put the correct and incorrect options in an array, gave it…
gerard
  • 221
  • 1
  • 2
  • 10
2
votes
1 answer

How do I use like & unlike functionality on every element while using map to render those elements?

I have social media posts stored in firebase and each post has a like button. I am using map() function to render those posts but I have declared a single state for like button using useState(). When I click on like button in any of the posts, the…
2
votes
2 answers

NextJS not mounting page content on Server Side even using getStaticProps (useEffect and useState)

I was developing a website where all the site sections were rendered from a JSON payload. In other words, the JSON had information such as the name of the section component to which it refers and attributes referring to the properties that must fill…
2
votes
2 answers

Can I use useState hook in class component?

I'm trying to solve a React problem on testdome. This is the task: The Message component contains an anchor element and a paragraph below the anchor. Rendering of the paragraph should be toggled by clicking on the anchor element using the following…
reactjs
  • 33
  • 4
2
votes
1 answer

How to use "useContext" in typescript?

I am trying to make a dark/light theme system in my project, but I am having some problems with the code. This line of code works fine in javascript: const [darktheme, setDarkTheme] = useContext(ThemeContext); But when I write it into typescript, I…
James Gaunt
  • 119
  • 1
  • 14
2
votes
1 answer

react page doesnt render when getting the data back from server

react won't work when rendering the page, but when i changed code in the vscode (added a line of console or comment one line out), the page is rendered. or when page is not rendered. when i hit refresh. i can see some of my content but it won't…
2
votes
2 answers

Is there a way to dynamically choose which state I want to update in react?

For example if I have a bunch of useState hooks such as: const [name, setName] = useState('') const [age, setAge] = useState(0) const [location, setLocation] = useState('') and a function that takes a string value such as: const handleClick =…
sgt_pepper85
  • 421
  • 4
  • 14
2
votes
4 answers

How to declare useState() initial value as null and then give it an object value later?

As can be seen in the image, I want to declare a react state hook to the alert of my webpage. At first, I want that there is no alert shown. And whenever user is clicking some button, I want to show a success alert with a message (error alert if…
Bittu Joshi
  • 110
  • 2
  • 10
2
votes
0 answers

ReactJS update a component stored in state array

I'm pretty new to coding so I hope my question won't sound too ridiculous. I've spent days to figure something out but didn't find anything. So I have a react page with the main component that is my "planning container". In this component, I have a…
BadBadDev
  • 21
  • 2