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

288
Visualizações
Checkbox select can't change after set fetch data true useeffect in react

I need to set the default value to my react material UI check box. So I did this task using React.useEfect hook.

This is how it looks like after setting the value using useEfect. enter image description here

But now I am facing a problem. After I unchecked my check box, I can't check again. And I have no idea what going on with my code. If anyone can help me with this, I am really thankful for that.

const [checked, setChecked] = React.useState({});
const variant= useSelector((state) => state.promoCodesReducer.selectedPromo);
React.useEffect(()=>{
   variant &&  variant.filter((filterData)=> 
   setChecked((newValues) => ({ ...newValues, [filterData.variantId]:checked })))

  },[row,variant])

My Check Box Component :

function addVariantSection(data) {
    return (
      <Grid key={data._id} container spacing={1}>
        <Grid item xs={12} style={{ justifyContent: 'space-between', display: 'flex', flexDirection: 'row' }}>
          <div style={{ margin: 10 }}>{`${data.productName ? data.productName : itemName}-${data.variant1} ${data.variant2}`}</div>
          <div>
            <Checkbox
              // eslint-disable-next-line no-useless-concat
              id={`${data.variantId ? data.variantId:data._id}-` + `checkData`}
              color='primary'
              // checked={checked[data._id ? data._id:data.variantId]}
              checked={checked[data.variantId ? data.variantId:data._id]}
              onChange={handleChecked(data.variantId ? data.variantId:data._id,data.productId)}
              style={{ margin: 10 }}
            />
          </div>

        </Grid>
      </Grid>
    );
  }

Methord for check box Value :

const handleChecked = (id,pid) => (e) => {
    const { checkedID } = e.target;
    const vID = e.currentTarget.id.split('-', 1);
    setChecked((values) => ({ ...values, [id]: checkedID }));
  };
about 4 years ago · Juan Pablo Isaza
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