Issues with useReducer not synchronously updating the state Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I update each dependency in package.json to the latest version?How to show a loading indicator in React Redux app while fetching the data?React Native with Redux - state changes not showing in consoleReact/Redux: modified state is not updated in viewRedux state doesn't update with action dispatchReact-Redux connected component doesn't get updated after state is changedmapDispatchToProps dispatch action not working to update StateReactJs + Redux - Reducer is working/ state not updatingReact component not updating when redux state is updatinguseState vs useReducer
/bin/ls sorts differently than just ls
2 sample t test for sample sizes - 30,000 and 150,000
Putting Ant-Man on house arrest
Converting a text document with special format to Pandas DataFrame
Why does BitLocker not use RSA?
What helicopter has the most rotor blades?
Etymology of 見舞い
Why did Israel vote against lifting the American embargo on Cuba?
What were wait-states, and why was it only an issue for PCs?
Protagonist's race is hidden - should I reveal it?
Is Bran literally the world's memory?
Meaning of this sentence, confused by まで
Knights and Knaves question
How to make an animal which can only breed for a certain number of generations?
Trying to enter the Fox's den
What is the definining line between a helicopter and a drone a person can ride in?
Does traveling In The United States require a passport or can I use my green card if not a US citizen?
Can I ask an author to send me his ebook?
A German immigrant ancestor has a "Registration Affidavit of Alien Enemy" on file. What does that mean exactly?
Has a Nobel Peace laureate ever been accused of war crimes?
Can a Wizard take the Magic Initiate feat and select spells from the Wizard list?
Providing direct feedback to a product salesperson
Construct a nonabelian group of order 44
Does Prince Arnaud cause someone holding the Princess to lose?
Issues with useReducer not synchronously updating the state
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I update each dependency in package.json to the latest version?How to show a loading indicator in React Redux app while fetching the data?React Native with Redux - state changes not showing in consoleReact/Redux: modified state is not updated in viewRedux state doesn't update with action dispatchReact-Redux connected component doesn't get updated after state is changedmapDispatchToProps dispatch action not working to update StateReactJs + Redux - Reducer is working/ state not updatingReact component not updating when redux state is updatinguseState vs useReducer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
According to React
docs :
useReducer is usually preferable to useState when you have complex
state logic that involves multiple sub-values or when the next state
depends on the previous one.
1. can somebody explain me why useReducer
is not updating the state synchronously ?
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
console.log(`STATE IN REDUCER`, [...state, action.path]) // => ["1.1"]
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
console.log(`STATE`, state) // => []
// here i want to do some stuff based on the lastest updated state (["1.1"] and not [])
// for example dispatch an action with redux
/>
2. How can I do some stuff (dispatch a redux action) based on the lastest updated state (["1.1"]
and not []
) ?
javascript reactjs
|
show 2 more comments
According to React
docs :
useReducer is usually preferable to useState when you have complex
state logic that involves multiple sub-values or when the next state
depends on the previous one.
1. can somebody explain me why useReducer
is not updating the state synchronously ?
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
console.log(`STATE IN REDUCER`, [...state, action.path]) // => ["1.1"]
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
console.log(`STATE`, state) // => []
// here i want to do some stuff based on the lastest updated state (["1.1"] and not [])
// for example dispatch an action with redux
/>
2. How can I do some stuff (dispatch a redux action) based on the lastest updated state (["1.1"]
and not []
) ?
javascript reactjs
The state is not updated synchronously, and even if it was React will not mutate the current state, sostate
is a reference to the previous state.
– Tholle
Mar 22 at 14:15
Because state updates are asynchronous. 99.9% sure using hooks doesn't change that. Also, as @Tholle said, thestate
you're referring to would be stale anyway.
– T.J. Crowder
Mar 22 at 14:17
@Tholle and how to solve that (look at the second part in the question)
– Youssef
Mar 22 at 14:19
@T.J.Crowder how to solve it then ?
– Youssef
Mar 22 at 14:20
1
@Youssef - Your component will get rerendered when the state changes. So just render according to state, as usual.
– T.J. Crowder
Mar 22 at 14:22
|
show 2 more comments
According to React
docs :
useReducer is usually preferable to useState when you have complex
state logic that involves multiple sub-values or when the next state
depends on the previous one.
1. can somebody explain me why useReducer
is not updating the state synchronously ?
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
console.log(`STATE IN REDUCER`, [...state, action.path]) // => ["1.1"]
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
console.log(`STATE`, state) // => []
// here i want to do some stuff based on the lastest updated state (["1.1"] and not [])
// for example dispatch an action with redux
/>
2. How can I do some stuff (dispatch a redux action) based on the lastest updated state (["1.1"]
and not []
) ?
javascript reactjs
According to React
docs :
useReducer is usually preferable to useState when you have complex
state logic that involves multiple sub-values or when the next state
depends on the previous one.
1. can somebody explain me why useReducer
is not updating the state synchronously ?
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
console.log(`STATE IN REDUCER`, [...state, action.path]) // => ["1.1"]
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
console.log(`STATE`, state) // => []
// here i want to do some stuff based on the lastest updated state (["1.1"] and not [])
// for example dispatch an action with redux
/>
2. How can I do some stuff (dispatch a redux action) based on the lastest updated state (["1.1"]
and not []
) ?
javascript reactjs
javascript reactjs
edited Mar 22 at 14:24
Youssef
asked Mar 22 at 14:12
YoussefYoussef
1,667154890
1,667154890
The state is not updated synchronously, and even if it was React will not mutate the current state, sostate
is a reference to the previous state.
– Tholle
Mar 22 at 14:15
Because state updates are asynchronous. 99.9% sure using hooks doesn't change that. Also, as @Tholle said, thestate
you're referring to would be stale anyway.
– T.J. Crowder
Mar 22 at 14:17
@Tholle and how to solve that (look at the second part in the question)
– Youssef
Mar 22 at 14:19
@T.J.Crowder how to solve it then ?
– Youssef
Mar 22 at 14:20
1
@Youssef - Your component will get rerendered when the state changes. So just render according to state, as usual.
– T.J. Crowder
Mar 22 at 14:22
|
show 2 more comments
The state is not updated synchronously, and even if it was React will not mutate the current state, sostate
is a reference to the previous state.
– Tholle
Mar 22 at 14:15
Because state updates are asynchronous. 99.9% sure using hooks doesn't change that. Also, as @Tholle said, thestate
you're referring to would be stale anyway.
– T.J. Crowder
Mar 22 at 14:17
@Tholle and how to solve that (look at the second part in the question)
– Youssef
Mar 22 at 14:19
@T.J.Crowder how to solve it then ?
– Youssef
Mar 22 at 14:20
1
@Youssef - Your component will get rerendered when the state changes. So just render according to state, as usual.
– T.J. Crowder
Mar 22 at 14:22
The state is not updated synchronously, and even if it was React will not mutate the current state, so
state
is a reference to the previous state.– Tholle
Mar 22 at 14:15
The state is not updated synchronously, and even if it was React will not mutate the current state, so
state
is a reference to the previous state.– Tholle
Mar 22 at 14:15
Because state updates are asynchronous. 99.9% sure using hooks doesn't change that. Also, as @Tholle said, the
state
you're referring to would be stale anyway.– T.J. Crowder
Mar 22 at 14:17
Because state updates are asynchronous. 99.9% sure using hooks doesn't change that. Also, as @Tholle said, the
state
you're referring to would be stale anyway.– T.J. Crowder
Mar 22 at 14:17
@Tholle and how to solve that (look at the second part in the question)
– Youssef
Mar 22 at 14:19
@Tholle and how to solve that (look at the second part in the question)
– Youssef
Mar 22 at 14:19
@T.J.Crowder how to solve it then ?
– Youssef
Mar 22 at 14:20
@T.J.Crowder how to solve it then ?
– Youssef
Mar 22 at 14:20
1
1
@Youssef - Your component will get rerendered when the state changes. So just render according to state, as usual.
– T.J. Crowder
Mar 22 at 14:22
@Youssef - Your component will get rerendered when the state changes. So just render according to state, as usual.
– T.J. Crowder
Mar 22 at 14:22
|
show 2 more comments
1 Answer
1
active
oldest
votes
Use useEffect
to access the state correctly. You could add some safe-guarding if you want something invoking if a certain criterion is hit.
If you want to access your reducer across components, you can store the reducer using Context API. Look below for an example. You can see the reducer being injected into the Context on the parent component and then two child components that a) dispatches an action b) receives the update from the action.
1. Example of context reducer to use across multiple components
import React from "react";
import ReactDOM from "react-dom";
const Application = React.createContext(
state: null,
dispatch: null
);
function ActionComponent()
const dispatch = React.useContext(Application);
return (
<div>
<div>Action Component</div>
<button onClick=() => dispatch("lol")>Do something</button>
</div>
);
function ListenerComponent()
const state = React.useContext(Application);
React.useEffect(
() =>
console.log(state);
,
[state]
);
return <div>Listener Component</div>;
function App()
const [state, dispatch] = React.useReducer(function(state = [], action)
return [...state, action];
);
return (
<Application.Provider value= state, dispatch >
<div className="App">
<ActionComponent />
<ListenerComponent />
</div>
</Application.Provider>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
2. Example of local reducer without using Application Context
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
React.useEffect(() =>
console.log(state);
, [state]);
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
/>
thanks @Win for this hint it will help me in the future, but i have thedispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?
– Youssef
Mar 22 at 14:30
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55301495%2fissues-with-usereducer-not-synchronously-updating-the-state%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use useEffect
to access the state correctly. You could add some safe-guarding if you want something invoking if a certain criterion is hit.
If you want to access your reducer across components, you can store the reducer using Context API. Look below for an example. You can see the reducer being injected into the Context on the parent component and then two child components that a) dispatches an action b) receives the update from the action.
1. Example of context reducer to use across multiple components
import React from "react";
import ReactDOM from "react-dom";
const Application = React.createContext(
state: null,
dispatch: null
);
function ActionComponent()
const dispatch = React.useContext(Application);
return (
<div>
<div>Action Component</div>
<button onClick=() => dispatch("lol")>Do something</button>
</div>
);
function ListenerComponent()
const state = React.useContext(Application);
React.useEffect(
() =>
console.log(state);
,
[state]
);
return <div>Listener Component</div>;
function App()
const [state, dispatch] = React.useReducer(function(state = [], action)
return [...state, action];
);
return (
<Application.Provider value= state, dispatch >
<div className="App">
<ActionComponent />
<ListenerComponent />
</div>
</Application.Provider>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
2. Example of local reducer without using Application Context
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
React.useEffect(() =>
console.log(state);
, [state]);
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
/>
thanks @Win for this hint it will help me in the future, but i have thedispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?
– Youssef
Mar 22 at 14:30
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
add a comment |
Use useEffect
to access the state correctly. You could add some safe-guarding if you want something invoking if a certain criterion is hit.
If you want to access your reducer across components, you can store the reducer using Context API. Look below for an example. You can see the reducer being injected into the Context on the parent component and then two child components that a) dispatches an action b) receives the update from the action.
1. Example of context reducer to use across multiple components
import React from "react";
import ReactDOM from "react-dom";
const Application = React.createContext(
state: null,
dispatch: null
);
function ActionComponent()
const dispatch = React.useContext(Application);
return (
<div>
<div>Action Component</div>
<button onClick=() => dispatch("lol")>Do something</button>
</div>
);
function ListenerComponent()
const state = React.useContext(Application);
React.useEffect(
() =>
console.log(state);
,
[state]
);
return <div>Listener Component</div>;
function App()
const [state, dispatch] = React.useReducer(function(state = [], action)
return [...state, action];
);
return (
<Application.Provider value= state, dispatch >
<div className="App">
<ActionComponent />
<ListenerComponent />
</div>
</Application.Provider>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
2. Example of local reducer without using Application Context
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
React.useEffect(() =>
console.log(state);
, [state]);
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
/>
thanks @Win for this hint it will help me in the future, but i have thedispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?
– Youssef
Mar 22 at 14:30
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
add a comment |
Use useEffect
to access the state correctly. You could add some safe-guarding if you want something invoking if a certain criterion is hit.
If you want to access your reducer across components, you can store the reducer using Context API. Look below for an example. You can see the reducer being injected into the Context on the parent component and then two child components that a) dispatches an action b) receives the update from the action.
1. Example of context reducer to use across multiple components
import React from "react";
import ReactDOM from "react-dom";
const Application = React.createContext(
state: null,
dispatch: null
);
function ActionComponent()
const dispatch = React.useContext(Application);
return (
<div>
<div>Action Component</div>
<button onClick=() => dispatch("lol")>Do something</button>
</div>
);
function ListenerComponent()
const state = React.useContext(Application);
React.useEffect(
() =>
console.log(state);
,
[state]
);
return <div>Listener Component</div>;
function App()
const [state, dispatch] = React.useReducer(function(state = [], action)
return [...state, action];
);
return (
<Application.Provider value= state, dispatch >
<div className="App">
<ActionComponent />
<ListenerComponent />
</div>
</Application.Provider>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
2. Example of local reducer without using Application Context
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
React.useEffect(() =>
console.log(state);
, [state]);
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
/>
Use useEffect
to access the state correctly. You could add some safe-guarding if you want something invoking if a certain criterion is hit.
If you want to access your reducer across components, you can store the reducer using Context API. Look below for an example. You can see the reducer being injected into the Context on the parent component and then two child components that a) dispatches an action b) receives the update from the action.
1. Example of context reducer to use across multiple components
import React from "react";
import ReactDOM from "react-dom";
const Application = React.createContext(
state: null,
dispatch: null
);
function ActionComponent()
const dispatch = React.useContext(Application);
return (
<div>
<div>Action Component</div>
<button onClick=() => dispatch("lol")>Do something</button>
</div>
);
function ListenerComponent()
const state = React.useContext(Application);
React.useEffect(
() =>
console.log(state);
,
[state]
);
return <div>Listener Component</div>;
function App()
const [state, dispatch] = React.useReducer(function(state = [], action)
return [...state, action];
);
return (
<Application.Provider value= state, dispatch >
<div className="App">
<ActionComponent />
<ListenerComponent />
</div>
</Application.Provider>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
2. Example of local reducer without using Application Context
const reducer = (state, action) =>
if( action.type === 'ADD_VALUE')
return [...state, action.path]
const [state, dispatch] = useReducer(reducer, [])
React.useEffect(() =>
console.log(state);
, [state]);
<input type="button" onClick=() =>
dispatch( type: 'ADD_VALUE', path: "1.1")
/>
edited Mar 22 at 14:47
answered Mar 22 at 14:21
WinWin
3,1481815
3,1481815
thanks @Win for this hint it will help me in the future, but i have thedispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?
– Youssef
Mar 22 at 14:30
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
add a comment |
thanks @Win for this hint it will help me in the future, but i have thedispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?
– Youssef
Mar 22 at 14:30
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
thanks @Win for this hint it will help me in the future, but i have the
dispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?– Youssef
Mar 22 at 14:30
thanks @Win for this hint it will help me in the future, but i have the
dispatch
inside a loop so i don't think this solution will be suitable for my case don't you think ?– Youssef
Mar 22 at 14:30
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
@Youssef it doesn't matter, I'll show you an example of how to talk to the same reducer from different components
– Win
Mar 22 at 14:31
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
Stack Snippets support React, including JSX; here's how to do one. Perhaps you could make your examples runnable?
– T.J. Crowder
Mar 22 at 14:34
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
@Youssef It does not matter if it's in a loop as the action is sent to a queue and the listener will receive the updated state once this loop has completed.
– Win
Mar 22 at 14:40
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
thanks a lot @Win i will take a look deeply in this and i will give you feedback
– Youssef
Mar 22 at 14:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55301495%2fissues-with-usereducer-not-synchronously-updating-the-state%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The state is not updated synchronously, and even if it was React will not mutate the current state, so
state
is a reference to the previous state.– Tholle
Mar 22 at 14:15
Because state updates are asynchronous. 99.9% sure using hooks doesn't change that. Also, as @Tholle said, the
state
you're referring to would be stale anyway.– T.J. Crowder
Mar 22 at 14:17
@Tholle and how to solve that (look at the second part in the question)
– Youssef
Mar 22 at 14:19
@T.J.Crowder how to solve it then ?
– Youssef
Mar 22 at 14:20
1
@Youssef - Your component will get rerendered when the state changes. So just render according to state, as usual.
– T.J. Crowder
Mar 22 at 14:22