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

213
Visualizações
React: can't add an array of objects to the product

I'm trying to add the selected attributes(radio buttons) to the product.ps:// when im not using this.setState in the handleChange function , this almost works, except for the fact that instead of getting an array of multiple objects with selected attributes, I get an array of single object with multiple keys/values.

render() {
    
    let product = this.state.currentProduct;
    let selectedAttributes = [];
    let testobj = {};

 let handleChange = (evt,attribute) => {
      let test = Object.assign({}, product);
      testobj[attribute] = evt.target.value;
      selectedAttributes.push(testobj);
      let noDuplicates = [...new Set(selectedAttributes)];
      test.selectedAttributes = noDuplicates;
      this.setState((state) => ({
        currentProduct: test,
      })) 
    }

return (
      <>
                <ul className={`attributesList ${attribute.id}`} key={attribute.id}>
                  {product.attributes[i].items.map((item) => {
                    return (  
                      <li>
                        <input className={attribute.id} onChange={(evt) => handleChange(evt,attribute.id)} type="radio" id={`attribute ${attribute.id} ${item.id}`} name={`attributesList ${attribute.id}`} value={item.displayValue}/> 
                        <label className={`attribute ${item.id}`} style={{backgroundColor: item.value}} htmlFor={`attribute ${attribute.id} ${item.id}`}>{item.displayValue}</label>
                      </li>
                    )
                  })}
                  </ul>
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