React setState doesn't work with Listener patternLoop inside React JSXCan you force a React component to rerender without calling setState?Programmatically navigate using react routerReactJS setState only works when nested inside setStateWhy is setState in reactjs Async instead of Sync?Update State in React (Firebase) Undefined Valuereact setstate not working when pushing elements to arrayReact-Native: State is not updating with setState?setState() bug when triggered from child component with callbackReact setState() after fetch not rerendering
Making polynomials representing frequency of a character in a list
What steps should I take to lawfully visit the United States as a tourist immediately after visiting on a B-1 visa?
How can I calculate the sum of 2 random dice out of a 3d6 pool in AnyDice?
Should disabled buttons give feedback when clicked?
How were Martello towers supposed to work?
How did the hit man miss?
Contexte et orthographe du mot « feedback »
Machine learning and operations research projects
Can the Mage Hand cantrip be used to trip an enemy who is running away?
Is there any word for "disobedience to God"?
Is anyone advocating the promotion of homosexuality in UK schools?
What prevents someone from claiming to be the murderer in order to get the real murderer off?
Cracking the Coding Interview — 1.5 One Away
Single word for "refusing to move to next activity unless present one is completed."
Credit score and financing new car
What's the point of having a RAID 1 configuration over incremental backups to a secondary drive?
How do you glue a text to a point?
Why didn't Thanos kill all the Dwarves on Nidavellir?
Why isn't there research to build a standard lunar, or Martian mobility platform?
Why are they 'nude photos'?
Is a request to book a business flight ticket for a graduate student an unreasonable one?
Print the last, middle and first character of your code
Shortest hex dumping program
Why didn't Nick Fury expose the villain's identity and plans?
React setState doesn't work with Listener pattern
Loop inside React JSXCan you force a React component to rerender without calling setState?Programmatically navigate using react routerReactJS setState only works when nested inside setStateWhy is setState in reactjs Async instead of Sync?Update State in React (Firebase) Undefined Valuereact setstate not working when pushing elements to arrayReact-Native: State is not updating with setState?setState() bug when triggered from child component with callbackReact setState() after fetch not rerendering
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
My app tries to login at first. And I have an isLoginTried state that lets us know whether login has tried. Entry point of the App sets the login result listener on Login Component. And Login component posts the result by calling that listener function.
And Listener function set state isLoginTried as true. But when I logged it after setState, isLoginTried is still false!
I know setState function is asynchronous so I logged it with callback function of setState. My code snippet is below. Please give me some advices. Thank you.
+MessageController is an instance. So It can keep the listener functions.
// GlobalApp.tsx
class GlobalApp extends Component {
constructor()
super();
this.state=
isLoginTried: false,
MessageController.setLoginResultListener(this.onLoginResult.bind(this));
...
onLoginResult(user: User)
this.setState(
user,
isLoginTried: true,
, () =>
console.log(this.state.isLoginTried); // it tells it's false!!
);
class UserDC()
login()
...
// This function always triggered
// no matter login has been successful or not
MessageController.postLoginResultListener(user);
onLoginResult of GlobalApp is executed! But it doesn't change state!
reactjs
add a comment |
My app tries to login at first. And I have an isLoginTried state that lets us know whether login has tried. Entry point of the App sets the login result listener on Login Component. And Login component posts the result by calling that listener function.
And Listener function set state isLoginTried as true. But when I logged it after setState, isLoginTried is still false!
I know setState function is asynchronous so I logged it with callback function of setState. My code snippet is below. Please give me some advices. Thank you.
+MessageController is an instance. So It can keep the listener functions.
// GlobalApp.tsx
class GlobalApp extends Component {
constructor()
super();
this.state=
isLoginTried: false,
MessageController.setLoginResultListener(this.onLoginResult.bind(this));
...
onLoginResult(user: User)
this.setState(
user,
isLoginTried: true,
, () =>
console.log(this.state.isLoginTried); // it tells it's false!!
);
class UserDC()
login()
...
// This function always triggered
// no matter login has been successful or not
MessageController.postLoginResultListener(user);
onLoginResult of GlobalApp is executed! But it doesn't change state!
reactjs
1
isLoginTired is spelled wrong, fix that first
– JREAM
Mar 26 at 2:37
@JREAM Thank you. It might've been tired....
– Kwoncharles
Mar 26 at 3:16
add a comment |
My app tries to login at first. And I have an isLoginTried state that lets us know whether login has tried. Entry point of the App sets the login result listener on Login Component. And Login component posts the result by calling that listener function.
And Listener function set state isLoginTried as true. But when I logged it after setState, isLoginTried is still false!
I know setState function is asynchronous so I logged it with callback function of setState. My code snippet is below. Please give me some advices. Thank you.
+MessageController is an instance. So It can keep the listener functions.
// GlobalApp.tsx
class GlobalApp extends Component {
constructor()
super();
this.state=
isLoginTried: false,
MessageController.setLoginResultListener(this.onLoginResult.bind(this));
...
onLoginResult(user: User)
this.setState(
user,
isLoginTried: true,
, () =>
console.log(this.state.isLoginTried); // it tells it's false!!
);
class UserDC()
login()
...
// This function always triggered
// no matter login has been successful or not
MessageController.postLoginResultListener(user);
onLoginResult of GlobalApp is executed! But it doesn't change state!
reactjs
My app tries to login at first. And I have an isLoginTried state that lets us know whether login has tried. Entry point of the App sets the login result listener on Login Component. And Login component posts the result by calling that listener function.
And Listener function set state isLoginTried as true. But when I logged it after setState, isLoginTried is still false!
I know setState function is asynchronous so I logged it with callback function of setState. My code snippet is below. Please give me some advices. Thank you.
+MessageController is an instance. So It can keep the listener functions.
// GlobalApp.tsx
class GlobalApp extends Component {
constructor()
super();
this.state=
isLoginTried: false,
MessageController.setLoginResultListener(this.onLoginResult.bind(this));
...
onLoginResult(user: User)
this.setState(
user,
isLoginTried: true,
, () =>
console.log(this.state.isLoginTried); // it tells it's false!!
);
class UserDC()
login()
...
// This function always triggered
// no matter login has been successful or not
MessageController.postLoginResultListener(user);
onLoginResult of GlobalApp is executed! But it doesn't change state!
reactjs
reactjs
edited Mar 26 at 3:16
Kwoncharles
asked Mar 26 at 2:14
KwoncharlesKwoncharles
83 bronze badges
83 bronze badges
1
isLoginTired is spelled wrong, fix that first
– JREAM
Mar 26 at 2:37
@JREAM Thank you. It might've been tired....
– Kwoncharles
Mar 26 at 3:16
add a comment |
1
isLoginTired is spelled wrong, fix that first
– JREAM
Mar 26 at 2:37
@JREAM Thank you. It might've been tired....
– Kwoncharles
Mar 26 at 3:16
1
1
isLoginTired is spelled wrong, fix that first
– JREAM
Mar 26 at 2:37
isLoginTired is spelled wrong, fix that first
– JREAM
Mar 26 at 2:37
@JREAM Thank you. It might've been tired....
– Kwoncharles
Mar 26 at 3:16
@JREAM Thank you. It might've been tired....
– Kwoncharles
Mar 26 at 3:16
add a comment |
0
active
oldest
votes
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%2f55348916%2freact-setstate-doesnt-work-with-listener-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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%2f55348916%2freact-setstate-doesnt-work-with-listener-pattern%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
1
isLoginTired is spelled wrong, fix that first
– JREAM
Mar 26 at 2:37
@JREAM Thank you. It might've been tired....
– Kwoncharles
Mar 26 at 3:16