Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [reactjs]

React is a JavaScript library for building user interfaces. It uses a declarative, component-based paradigm and aims to be efficient and flexible.

863 votes
22 answers
857k views

The useState set method is not reflecting a change immediately

I am trying to learn hooks and the useState method has made me confused. I am assigning an initial value to a state in the form of an array. The set method in useState is not working for me, both with ...
Pranjal's user avatar
  • 8,743
25 votes
7 answers
35k views

How to create a protected route with react-router-dom?

How to create a protected route with react-router-dom and storing the response in localStorage, so that when a user tries to open next time they can view their details again. After login, they should ...
learner62's user avatar
  • 590
133 votes
7 answers
93k views

Why useEffect running twice and how to handle it well in React?

I have a counter and a console.log() in an useEffect to log every change in my state, but the useEffect is getting called two times on mount. I am using React 18. Here is a CodeSandbox of my project ...
hossein fti's user avatar
  • 1,430
427 votes
10 answers
262k views

Why does calling react setState method not mutate the state immediately?

I'm reading Forms section of reactjs documentation and just tried this code to demonstrate onChange usage (JSBIN). var React= require('react'); var ControlledForm= React.createClass({ ...
Salah Eddine Taouririt's user avatar
1066 votes
31 answers
1.3m views

Understanding unique keys for array children in React.js

I'm building a React component that accepts a JSON data source and creates a sortable table. Each of the dynamic data rows has a unique key assigned to it but I'm still getting an error of: Each ...
Brett DeWoody's user avatar
1278 votes
63 answers
871k views

React-router URLs don't work when refreshing or writing manually

I'm using React-router and it works fine while I'm clicking on link buttons, but when I refresh my webpage it does not load what I want. For instance, I am in localhost/joblist and everything is fine ...
DavidDev's user avatar
  • 13.1k
12 votes
5 answers
20k views

How to pass data from a page to another page using react router

Please I need help on react-router-dom, I am new to the library and can seem to find any solution since. I am getting three results from an api call, in which I map over the data to render it on UI, ...
Shadow_net's user avatar
2335 votes
49 answers
1.7m views

How to programmatically navigate using React Router?

With react-router I can use the Link element to create links which are natively handled by react router. I see internally it calls this.context.transitionTo(...). I want to do a navigation. Not ...
George Mauer's user avatar
144 votes
7 answers
82k views

Why can't I directly modify a component's state, really?

I understand that React tutorials and documentation warn in no uncertain terms that state should not be directly mutated and that everything should go through setState. I would like to understand why, ...
Marcus Junius Brutus's user avatar
173 votes
15 answers
210k views

setState doesn't update the state immediately [duplicate]

I would like to ask why my state is not changing when I do an onClick event. I've search a while ago that I need to bind the onClick function in constructor but still the state is not updating. Here's ...
Sydney Loteria's user avatar
214 votes
11 answers
467k views

React setState not updating state

So I have this: let total = newDealersDeckTotal.reduce(function(a, b) { return a + b; }, 0); console.log(total, 'tittal'); //outputs correct total setTimeout(() => { this.setState({ ...
The worm's user avatar
  • 5,830
631 votes
36 answers
495k views

How to update nested state properties in React

I'm trying to organize my state by using nested property like this: this.state = { someProperty: { flag:true } } But updating state like this, this.setState({ someProperty.flag: false })...
Alex Yong's user avatar
  • 7,535
618 votes
19 answers
931k views

Correct modification of state arrays in React.js

I want to add an element to the end of a state array, is this the correct way to do it? this.state.arrayvar.push(newelement); this.setState({ arrayvar:this.state.arrayvar }); I'm concerned that ...
fadedbee's user avatar
  • 44.1k
189 votes
9 answers
191k views

Why is setState in reactjs Async instead of Sync?

I have just found that in react this.setState() function in any component is asynchronous or is called after the completion of the function that it was called in. Now I searched and found this blog (...
Anup's user avatar
  • 2,414
2054 votes
85 answers
2.1m views

Loop inside React JSX

I'm trying to do something like the following in React JSX (where ObjectRow is a separate component): <tbody> for (var i=0; i < numrows; i++) { <ObjectRow/> } </...
B Robster's user avatar
  • 41.7k

15 30 50 per page
1
2 3 4 5
1674