JavaScript Strange behavior of React setState with let variable change

The code below will render a <h1> and two <button>.

In my expectation, the changeString1 <button> will change letString to 1 and change <h1> text to 1 finally, while the changeString2 <button> will

  1. change <h1> text to 3 if I click the changeString2 first
  2. change <h1> text to 1 if I click the changeString1 first

But in fact

  1. If I click the changeString1 once first, then click the changeString2, <h1> text will be 3 ! But why?
  2. What's more, if I click the changeString1 twice first, then click the changeString2, <h1> text will be 1 ! But why?

It seems that both of 2 facts are contradictory...

You can test this by https://codesandbox.io/s/wy4l1y4o8

import React, { useState } from "react";
import ReactDOM from "react-dom";

function App() {
  let letString = "3";
  const [statString, setStatString] = useState("2");

  function changeString1() {
    letString = "1";
    setStatString(letString);
  }

  function changeString2() {
    console.log(letString);
    setStatString(letString);
  }

  return (
    <div>
      <h1>{statString}</h1>
      <button onClick={changeString1}>changeString1</button>
      <button onClick={changeString2}>changeString2</button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Answer:1

Please look at the flow that is taking place:-

1st click on changeString1

state changes

component re renders

letString gets re-initialized to 3 because of let letString = "3"

value of letString = 3;
value of statString = 1 (state);

2nd click on changeString1

value of letString gets set to 1 in the function call;

But value of statString(state) is already 1 so no state changes and component never re-renders and letString is not re-initialized

After 2nd click values are:-

letString = 1;
statString = 1(state);

Now, when you click on changeString2 value of letString is 1 and statString is also 1 so state doesn't changes and nothing happens and you just see 1.
Answer:2

I have two domains. [www.one.de and www.two.de] on www.one.de I have a script witch makes a ajaxcall to www.two.de/collect.php First I call on www.two.de this php-script to set a cookie: $domain ="....

I have two domains. [www.one.de and www.two.de] on www.one.de I have a script witch makes a ajaxcall to www.two.de/collect.php First I call on www.two.de this php-script to set a cookie: $domain ="....

  1. access cookies
  2. access cookies javascript
  3. access cookie from different domain
  4. access cookie react
  5. access cookie from server
  6. access cookie from iframe
  7. access cookies on iphone
  8. access cookies on android
  9. access cookie in php
  10. access cookies in angular
  11. access cookie from subdomain
  12. access cookies in jquery
  13. access cookies express
  14. access cookies in html
  15. access cookies on ipad
  16. access cookies from chrome extension
  17. access cookie in jsp
  18. access cookies from service worker
  19. access cookies from another domain
  20. access cookies in c#

I was trying the below commands on Chrome console. I am able to create the object using new(line 2 below) but using call doesn't work. Can anyone explain what could be the reason ? function ...

I was trying the below commands on Chrome console. I am able to create the object using new(line 2 below) but using call doesn't work. Can anyone explain what could be the reason ? function ...

I have the following method to paste some HTML at the caret position, using `range.insertNode(), sourced from this SO answer function insertAtCursor(html) { var sel, range; if (window....

I have the following method to paste some HTML at the caret position, using `range.insertNode(), sourced from this SO answer function insertAtCursor(html) { var sel, range; if (window....

  1. paste html at caret

I mostly see JavaScript use classes as a constructor as following: class Rectangle { constructor(height, width) { this.height = height; this.width = width; } // Getter get area() { ...

I mostly see JavaScript use classes as a constructor as following: class Rectangle { constructor(height, width) { this.height = height; this.width = width; } // Getter get area() { ...

  1. what is a national emergency
  2. whatsapp
  3. whataburger
  4. what time is it
  5. what is my ip
  6. what song is this
  7. what is a state of emergency
  8. what is a dry cough
  9. what happens in a national emergency
  10. what is the stafford act
  11. what is a bear market
  12. what does ���� mean
  13. what to watch
  14. what stocks to buy today
  15. whataburger menu
  16. what is the weather
  17. what is rsv
  18. what is gluten
  19. what is pv
  20. what is a boomer