Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Why changes in Checkbox's value are not displayed?

I'm using react and material ui. I need to get all checkboxes in a list checked by checking one checkbox in a parent component (i.e. I need to select all). I pass down the correct value of the parent checkbox via props, but that doesn't trigger visual changes in its children, even though their values do change to 'true'. I'm sure that the values are correct, because I tried logging them to the console.

Here's the parent:

const [checkboxValue, setCheckboxValue] = useState(false)

<Checkbox value={checkboxValue} onChange={e => setCheckboxValue(e.target.checked)}/>
{elements.map(element => (<Element selectAll={checkboxValue}/>))}

And here's the child:

function Element(props) {
    const [checkboxValue, setCheckboxValue] = useState(false)
    useEffect(() => {
        setCheckboxValue(props.selectAll)
    }, [props.selectAll])

return (
<Checkbox value={checkboxValue} onChange={e => setCheckboxValue(e.target.checked)}/>)
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you are using value instead of checked

function SingleCheckBox({ selectAll }) {
  const [checkboxValue, setCheckboxValue] = useState(selectAll);

  useEffect(() => {
    setCheckboxValue(selectAll);
  }, [selectAll]);

  return (
    <Checkbox
      checked={checkboxValue}
      onChange={(e) => setCheckboxValue(e.target.checked)}
    />
  );
}

export default function App() {
  const [checkboxValue, setCheckboxValue] = useState(false);

  return (
    <div className="App">
      <Checkbox
        checked={checkboxValue}
        onChange={(e) => {
          setCheckboxValue(e.target.checked);
        }}
      />
      <div>
        <h2>Other checkboxes</h2>
        <SingleCheckBox selectAll={checkboxValue} />
      </div>
    </div>
  );
}

https://codesandbox.io/s/angry-buck-yp7f3z?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda