React - Initial error in the checked Switch property (React-Switchery)

Asked

Viewed 112 times

0

I’m using the library’s Switch component React-Switchery in the React, on a work project. I used it on several screens without any problem, but in a specific screen the following occurs: even with the property "checked" being initialized with true value, the component does not appear as checked. And when you click on it for the first time, the visual state does not change (the change event is called normally, setting prop to false - in this case it is normal that the visual does not change, the problem is only with the initial visual status). From the second click, everything happens as expected.

P.S.: this is a screen that shows the details of a "client". Status 1 is for active, and 0 is for inactive.

Change event:

const onStatusChange = (checked: boolean) => {
    let cli: IClientDto = {...client, Status: checked ? 1 : 0}
    setClient(cli)
    setClientStatus(checked ? 1 : 0) // essa é a setagem importante nesse caso
  }

Switch component of "Active/Inactive":

<Switch 
  checked={clientStatus === 1}
  options={{
    color: '#00b0eb'
  }}
  onChange={checked => onStatusChange(checked)}
/>

Declaration of const clientStatus and set moment as true (client. Status is 1)

const [clientStatus, setClientStatus] = useState<number>(0)

useEffect(() => {
var pClient = Data.get<IClientDto>(`api/management/client/${Id}`)
...
Promise.all([pClient, ...]).then(
([client, ...]) => {
  ...
  setClientStatus(client.Status)
})
}, [])

Update: new unsuccessful attempts at problem resolution

Using a Boolean const instead of the numeric for switch state control

const [active, setActive] = useState<boolean>(false)

useEffect(() => {
var pClient = Data.get<IClientDto>(`api/management/client/${Id}`)
...
Promise.all([pClient, ...]).then(
([client, ...]) => {
  ...
  setActive(client.Status == 1)
})
}, [])

const onStatusChange = (checked: boolean) => {
  let cli: IClientDto = {...client, Status: checked ? 1 : 0}
  setClient(cli)
  setActive(checked)
}

<Switch 
  checked={active}
  options={{
    color: '#00b0eb'
  }}
  onChange={checked => onStatusChange(checked)}
/>

Keeping the boolean const in control, but treating the checked prop as "hardcoded" (two attempts)

{ active
   ? <Switch 
       checked={true}
       options={{
         color: '#00b0eb'
       }}
       onChange={checked => onStatusChange(checked)}
     />
   : <Switch 
       checked={false}
       options={{
         color: '#00b0eb'
       }}
       onChange={checked => onStatusChange(checked)}
     />
}
{ active
   ? <Switch 
       checked
       options={{
         color: '#00b0eb'
       }}
       onChange={checked => onStatusChange(checked)}
     />
   : <Switch 
       options={{
         color: '#00b0eb'
       }}
       onChange={checked => onStatusChange(checked)}
     />
}

If anyone can help me solve this error, I’d appreciate it.:P

  • Friend, I have no idea if this is your case, so I put as comment instead of response, but I once had a problem with switches that I discovered was because of the type of return. It was not getting the Boolean false, but the string "false". Checks if it is not your case.

  • Worse than that is not it, see... The api returns the status (active/inactive) in numbers. Still trying to solve the problem, I started using a boolean const to control the checked state, and then tried using the prop "checked" as hardcoded, but nothing helped. I will even supplement the question with these attempts. Anyway, I thank you for your help!

1 answer

0

Well, you can’t be sure without having the jsx here, but looking at your code and their lib, and the fact that your switch Component works in other cases, and it gives the toggle correctly...etc

const [clientStatus, setClientStatus] = useState<number>(0)

useEffect(() => {
var pClient = Data.get<IClientDto>(`api/management/client/${Id}`)
...
Promise.all([pClient, ...]).then(
([client, ...]) => {
  ...
  setClientStatus(client.Status)
})
}, [])

your problem is probably there, note that your state that stores the checked value of the switch comes from a precedent... ie it takes a while for it to have its value changed and when it is changed on account of your Switch has already been reengineered with the initial value of 0, it is disabled...

That being the problem, what you can do is place a loading while performing the call, and only re-enter the Switch when you finish loading.

  • Hi, thanks for your help! So here at JSX, there’s a loading. This is what you suggested, the Switch is only rendered after the result of the call has "arrived" (and is not null)

  • got it, and qnd you tested with hardcoding, what happened? it worked or not?

  • also did not work :/

  • kkkk if n worked even in hardcode, then we have a case of witchcraft, I would install another lib with Switcher, anything talks to the designer if you can change the model

  • What hatred kkkkkkkkk was thinking about it yesterday, but it is so absurd that it does not work only on a specific screen... anyway, vlw by help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.