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.
– Rômulo O. Torres
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!
– Guilherme Djrdjrjan Santos