Linked Questions
22 questions linked to/from Updating and merging state object using React useState() hook
0
votes
1
answer
228
views
Update multiple times same state from asynchronous callback [duplicate]
I'm trying to update the same state from an asynchronous callback launched in parallel. The problem is that each one of the asynchronous callback always has the init state and doesn't get the updated ...
1
vote
0
answers
61
views
Cannot read property 'length' of undefined in functional component while it works in class component [duplicate]
As the title says I have an error that says "cannot read property 'length' of undefined" when I'm handling change in FormInput. However, if I refactor my SignUp component from functional to ...
0
votes
0
answers
50
views
ForEach API calls react state not updating correctly [duplicate]
I've got an API to work with where I first need to get a list of ids before calling APIs to get more details of each item.
I'm having trouble storing the result of the details in state. Somethings ...
321
votes
19
answers
662k
views
React Hooks useState() with Object
What is the correct way of updating state, in a nested object, in React with Hooks?
export Example = () => {
const [exampleState, setExampleState] = useState(
{masterField: {
fieldOne: &...
8
votes
1
answer
23k
views
React useState update based on a variables previous value
The react.js site has the following code on this page:
const [count, setCount] = useState(0);
...
<button onClick={() => setCount(count + 1)}>
But since count is updated based on its ...
5
votes
2
answers
12k
views
How to execute appropriate useState dynamically from string names in react hooks?
Imagine a scenario that there's a same operation through different values (For instance generating a custom html element for different input values); in the case of using a component class, I would ...
3
votes
4
answers
2k
views
how to write a multi lines state with Hooks useState in ReactJs
React 16.9
I am aware that this class component state:
class JustAnotherCounter extends Component {
state = {
count: 0
};
is the equivalent of using Hooks useState:
function ...
1
vote
2
answers
5k
views
How to add new objects to an existing useState array using map() or foreach()?
I'm trying to create a Twitter clone, and I'm having troubles with my news feed. Basically it pulls tweets from my firebase database for each user followed by the current user. So say you follow Jon ...
0
votes
1
answer
6k
views
Uncaught TypeError: Cannot create property 'value' on string
I'm getting this error as soon as I input a second character:
Uncaught TypeError: Cannot create property 'value' on string 'a'
The code so far:
function App() {
const [object, setObject] = useState(...
2
votes
2
answers
1k
views
React - useState doesn't update state on setInterval
The state doesn't update the value even though I'm setting it to the oldvalue + 1.
When logging out the values of ltrNewValue or rtlNewValue it's always the same. It's as it's being overwritten by the ...
4
votes
2
answers
2k
views
In react hooks how do I pass a newly created object to a state object?
How do I pass err to errors : {} here ?
const data = { name: "", errors: {} }
const [values, setValues] = useState(data)
const handleSubmit = () => {
const err = validate(); // I receive an ...
0
votes
1
answer
2k
views
submit a form using patch in react js
i am trying to submit a form using patch in react js i want to merge Values & formData so that i can pass it a single variable throgh the api but i am getting error when i do that i have also ...
0
votes
2
answers
2k
views
Toggle booleans and update obj with useState()
Currently, I have many checkboxes which all obviously correlate to boolean values that need to be passed on onSubmit. I had something like this initially:
const [value, setValue] = React.useState({
...
2
votes
3
answers
834
views
Is there a way to update only one property of an object while keeping the others the same?
I am trying to make a basketball scoreboard app in React and have each team as it's own object with score, field goals made, and field goal attempts as properties. I have buttons that increment all of ...
0
votes
1
answer
407
views
React+JavaScript: Item appears on console but then disappears
I am trying to console log the two fields from my sign up page. However, the item appears in the console for about a second and then disappears. Any help is appreciated. Please find my code for the ...