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

Image not showing using useState hook in react

I used useState hook and called the memeData images with handler onClick on a button. I dont know the issue why it is not showing my image. Either there isn't any error in console or in terminal. Here is the image of my output Can anybody help???…
Warda
  • 1,226
  • 2
  • 11
  • 17
0
votes
1 answer

how to change modal based on select option value in reactjs

I want to show 2nd modal based on select option value: Here is my code:
Shawn
  • 1,232
  • 1
  • 14
  • 44
0
votes
1 answer

Why does my useEffect run multiple times even when that component should only be rendered once?

My useEffect is running multiple times per load even when the useEffect dependency isn't being updated. See console.log("filtering events"). Every time the page is loaded the useEffect runs three times (my best guess is because the components are…
0
votes
0 answers

useState/State unable to hold data on page refresh - React

The problem is when page is load through npm run dev command console.log(filter) show the data but as soon as I refresh the page all the data disappears and empty array is shown [] import "react-table-filter/lib/styles.css"; const TableFilter =…
AnumR
  • 85
  • 1
  • 12
0
votes
1 answer

How to setState from different triggers? Dynamically updating

I am currently trying to make an application within React that accepts a form. This form contains input fields that the user can type directly into and this will generate a state change in the formInput object: const [formInput, updateFormInput] =…
Kevin
  • 137
  • 1
  • 12
0
votes
2 answers

Add object to array inside an object(React useState)

I have a following state declared const [state, setState] = useState({ title: "", subtitle: "", products: [], state.products is an array of objects e.g. products = [{a: "", b: "", c: ""}] On form submit I want to take the…
ProjectAlice
  • 71
  • 1
  • 4
0
votes
1 answer

Passing state from _app to pages with typescript in next.js

I have a state which I would like to set in one page and then access in another page. My solution is to create the state in _app.tsx and then pass it to each page. My understanding is I can create the state in _app.tsx like this function MyApp({…
sev
  • 1,500
  • 17
  • 45
0
votes
2 answers

How can I update a setState from a parent using a child?

I'm learning React and trying to understand the concept of props and useState Hook working together. My goal was to make a simple counter and I started with the increase function of it, but I'm getting anywhere. App.js import Increase from…
guilhermxlopes
  • 138
  • 1
  • 7
0
votes
2 answers

setArray(array) not working in React UseEffect

When I log array I get the correct array. But after when I try to setNewArray(array) and then log newArray it gives me an empty array. This is for an appointment app where I want to compare available times and taken times to output an array with…
Walnut
  • 1
  • 2
0
votes
0 answers

After setting state in react an infinite loop starts

I am having an issue where an infinite loop start after setting new state in my app.js. I think best is to take a look at the code: const [favorites, setFavorites] = useState([]); const markAsFavorite = (currentCharacter) => { setFavorites({…
Simon CF
  • 25
  • 1
  • 1
  • 8
0
votes
1 answer

React functional component pass props to modal after state updates

I have a User page component which has a link that is meant to open a FollowingModal modal component to show the followers of a user. The User component calls an async function in my followers context to retieve the user's details of the user page I…
0
votes
1 answer

Problem while passing useState variable from one JSX file to another

(CreateColumn.jsx) import React, { useState, useEffect } from "react"; import Axios from "axios"; import "./styles.css"; function CreateColumn() { let [val1, setVal1] = useState("0"); let [val2, setVal2] = useState("0"); let [secName,…
0
votes
1 answer

React useState array empty on initial load but after editing code while app is running array fills?

This is going to be really hard to explain, but here goes. I am building a React card grid with a filter. The data is pulled from an MySQL AWS API I built. The .tags property is JSON with an array that stores each tag associated with the card. I…
masonjacob
  • 36
  • 9
0
votes
1 answer

React useState doesn't update even with useEffect added

Probably it is a classic issue with useState which is not updating. So there is a tree with some checkboxes, some of them are already checked as they map some data from an endpoint. The user has the possibility to check/uncheck them. There is a…
Leo Messi
  • 5,157
  • 14
  • 63
  • 125
0
votes
1 answer

How to set the formik field value with the useState hook?

I have a simple form which has only two fields,name and wallet_address, I want the user's to type wallet_address or simply scan the address, once the scan is successful the address will be stored in the state variable, I want to update the…
Mohanraj G
  • 85
  • 1
  • 9