0
I have a question when receiving the database data for my checkboxes. Even the value coming false
from the bank, the checkbox is checked.
I’ve tried several forum responses but I must be missing something.
I get json from the bank and looping using map
with each key
correspondent.
In the case below, host.host
would be equivalent to the returned json of res.data.host
:
data: {
host: "false"
}
Home js.
function Home() {
const { handleSubmit, register, errors } = useForm()
const [users, setUsers] = useState([])
const [file, setFile] = useState('')
useEffect(() => {
async function loadUser() {
const userId = localStorage.getItem('id')
const res = await axios.post(`${process.env.REACT_APP_GET_USER_DATA_URL}/react-getUserData.php`, { userId })
console.log(res)
setUsers([...users, res.data])
}
loadUser()
register({ name: 'file' })
}, [register])
function handleChange(e) {
const isChecked = e.target.checked
console.log(isChecked)
}
...
return (
<>
<label className="btn btn-circle border filters-btn">
{users.map((host) => (
<input
type="checkbox"
name="host"
id="host"
key={host}
defaultChecked={host.host}
ref={register}
onChange={handleChange}
/>
))}
<i></i>
<span>Hospedagem</span>
</label>
host
is in text so it’s true, it needs to behost: false
to make the expected effect, that is, the kind that is hindering its development.– novic