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;








1















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!










share|improve this question



















  • 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















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!










share|improve this question



















  • 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








1








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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













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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해