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

Update the form state date with new date

I'm using two different states in my Form component. One to capture all the text inputs and one to capture the Date using the DatePicker component from Material UI. I want to add a date state in my main input and update it based on the date…
mrahma04
  • 109
  • 1
  • 6
0
votes
1 answer

useState not updating all components of my app

Heyo. Working with react and I've encountered a puzzling issue. import { useState } from "react"; function useTheme() { const [themeName, setThemeName] = useState("dark"); return [themeName, setThemeName]; } function Other() { const…
0
votes
2 answers

useState executing before another function

I am trying to setRes() as respond which is the result for a login request but Res is not setting This is the function: const [res, setRes] = useState({ auth: false, type: '', reason: '' }) const Handlelogin = async (e) => { …
Firas SCMP
  • 461
  • 4
  • 18
0
votes
0 answers

Is there any benefit to storing a props value as a state variable in React Native?

Is the state variable redundant in the following React Native code? export default function Test(props) { const [number, setNumber] = useState(props.number); return( {number} ) } What is considered best practice,…
jpryor
  • 31
  • 1
  • 3
0
votes
1 answer

LocalStorage keeps changing my key-pair instead of pushing to the array

I want to add to the array a new object everytime I click at a card, but when I do so it changes the last key-pair to the new one and it doesnt add it. I chose this method of updating the state since I saw it is more popular than the one with the…
Liss
  • 23
  • 1
  • 5
0
votes
1 answer

how to display buffer images in react

i'm new with react and i have an endpoint that returns image as buffer and text, but i'm having issue converting that buffer to img url and displaying it, below is my code snippet
Tosin Ayoola
  • 77
  • 1
  • 9
0
votes
1 answer

Is there a way to pass a variable as an argument in onClick handler function in an element? - React JS

I'm trying to create a react app that displays a list of the basic projects I've done using react. What I'm trying to archive is that, upon clicking the "Click me" Button, it should replace the current component (The default component) with the…
Ankit Sharma
  • 164
  • 8
0
votes
2 answers

cannot change usestate to change the state of the components

This is code of my AddBlog Component import React, { useState } from "react"; function AddBlogs() { const[state, setState] = useState({blogs:[]}) const AddAnother = () => { return (
Roshan Ojha
  • 111
  • 4
  • 13
0
votes
2 answers

Can you pass in a useState function into a component on initialization?

I'm using the useState hook to manage rendering components on screen. I want to initialize it with a component while passing in the useState function to set the screen into the component. Here is my App.js. The error I get is in regards to passing a…
Ancross
  • 25
  • 4
0
votes
1 answer

Why we get error while set the useState method in react?

Here I am using the react usestate hook I have make the usestate as const { blogs, setBlogs } = useState([{postId:"",blogCity:"",blogTitle:"",username:"",time:""}]); Now I want to store multiple blogs in this usestate But the useEffect hook return…
user17048286
0
votes
1 answer

useState is not managing state in react native

I'm fetching data from Api, and then updating my state, but state is just holding last element of array, I'm using spread operator. const Index=()=>{ const [data, setData] = useState([]); //append new messages in this data const [keys,…
0
votes
0 answers

React dropdown state updates on the second click

I'm actually facing an issue where the setQuery is updating on the second click whereas the selected is updating instantly. I tried to use the prevState idea but still the issue persists. interface queryProps { intensity: string; categoryId:…
0
votes
1 answer

Uncaught TypeError: setState is not a function, why isn't it working?

I am using a dropdown box to map through some state data that is already established. I am wanting the user to be able to make a decision from the dropdown menu, which will then save to its own state... The code is this, as you can see the onChange…
Thom
  • 9
  • 1
0
votes
0 answers

I typed exactly same way but my code made TypeError like this. //useState in react code

I checked my code more than 4 times and rewrited entire code. but I couldn't find any difference between Youtube code and mine. here is the code import axios from 'axios'; import { useEffect, useState } from 'react'; import…
thebat
  • 21
0
votes
1 answer

Props.email value not printed on screen in react.js

See app.js and ternaryi.js I pass the email value in ternaryi and want to print on screen but only the count will be printed can anyone hep me to solve this problem. App.js import logo from "./logo.svg"; import "./App.css"; import React, {…
1 2 3
99
100