0
  import * as React from 'react'
  let [on, setOn] = React.useState(false)

  React.useEffect(() => {

      window.api.electronIpcSend("confsettings", "give-me-data-from-db");
      window.api.electronIpcOn("jitsiSetts-from-db", (event, args) => {
        console.log("conference.tsx-jitsiSetts-from-db-args.alwaysOnTopWindowEnable: ", args)
        let jitsiSetts = args
        console.log("conference.tsx-jitsiSetts.alwaysOnTopWindowEnable = args : ", jitsiSetts.alwaysOnTopWindowEnable)
        if (jitsiSetts.alwaysOnTopWindowEnable === 1) {
          console.log("It has to be set as TRUE")
          setOn(true)
          console.log("on: ", on)
        } else {
          console.log("It has to be set as FALSE")
          setOn(false)
          console.log("on: ", on)
        }
        console.log("on-2: ", on)
      })

    return () => { // clean-up function
    }
  }, [])

I get this strange output:

conference.tsx-jitsiSetts.alwaysOnTopWindowEnable = args :  1
It has to be set as TRUE
on:  false
on-2:  false

How to correctly set on with setOn to true?

Raphael10
  • 2,508
  • 7
  • 22
  • 50
  • 1
    You're setting it correctly, you just have your log statements in a place that isn't useful. Calling `setOn` does not (and can not) change the value in the local variable `on`. What calling `setOn` does is ask react to rerender the component. When that render eventually happens, a new local variable will be created with the new value, but code from the previous render can not see that. If you want to verify that the component has rerendered, put your log statement in the body of the component (eg, on the line after you call `useState(false)`) – Nicholas Tower Dec 14 '22 at 14:29
  • 1
    When you set the state in `useEffect()`, it is not reflected directly in your variable `on`. You only see the change upon a new render. But, `useEffect()` is only rendered once, you tell it so by using this `}, [])`. – four-eyes Dec 14 '22 at 14:29
  • Thank you @NicholasTower and thank you @four-eyes for your kind comments. Actually putting `console.log("on: ", on)` make the real updated value of the variable visible. I'm trying now to make this updated value visible also to the `default value` of a switch: } unCheckedChildren={} defaultChecked={on} onChange={AntdOnChange} /> – Raphael10 Dec 14 '22 at 15:21
  • By design, `defaultChecked` only matters on the very first render. Changing it afterwards will have no effect. Perhaps you want to use the `checked` property instead? – Nicholas Tower Dec 14 '22 at 15:23
  • @NicholasTower I would like to make the Switch value (true/false) equals to the value (true/false) I get from jitsiSetts.alwaysOnTopWindowEnable . This is why I tried in this way, without succeding : ``` ``` – Raphael10 Dec 14 '22 at 15:30
  • Changing the `defaultChecked` prop has no effect. Is there a reason you can't use `checked`? Does it need to be an uncontrolled component? – Nicholas Tower Dec 14 '22 at 15:38
  • @Nicholas Tower in this simple "demo" in CodeSandbox: https://codesandbox.io/s/cool-fast-fi426k?file=/src/App.tsx you can see that `checked` actually changes, based on an external condition (inputted text length), but this does not `"physically"`change the Switch – Raphael10 Dec 14 '22 at 17:26

0 Answers0