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

99
Visualizações
Updating multiple attributes of a nested state array of objects from a dynamic object

I am trying to update a nested state array with an object with dynamic keys but have something slightly off and was hoping someone may be able to help guide me in the right direction:

state array: 
[
   0: { createdTime: 'XXX',
        id: 'XXX',
        fields: {
           k1: v1,
           k2: v2,
           k3: v3,
      }
]

I have a functional component returning an object like:

e = { k2: v4 }

the follow code is close but a little off:

onInfoEditSubmit = (props, e, n) => {
    if (Object.keys(e).length > 0) {
        let dl = new DataLayer()
        dl.updateStartupRecord(props.current_startup_or_engagement.id, e)
        this.setState(prevState => ({

            startup_records: prevState.startup_records.map(
                r => r.id === props.current_startup_or_engagement.id ?
                    { ...r,
                        fields: {
                            ...r.fields,
                            e
                        }
                    }: r
            )

        }))
    }
}

new state array after this code:

state array: 
[
   0: { createdTime: 'XXX',
        id: 'XXX',
        fields: {
           k1: v1,
           k2: v2,
           k3: v3,
           e: {k2: v4}
      }
]

I was following this post: Updating an object with setState in React

In this example I am only updating a single property but would like this to work for multiple new values, so an input object might look like:

e = {k2: v4, k3: v5}

Can I do what I am trying to do with this method? Any help would be greatly appreciated, thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to use the spread operator for e as well:

onInfoEditSubmit = (props, e, n) => {
    if (Object.keys(e).length > 0) {
        let dl = new DataLayer()
        dl.updateStartupRecord(props.current_startup_or_engagement.id, e)
        this.setState(prevState => ({

            startup_records: prevState.startup_records.map(
                r => r.id === props.current_startup_or_engagement.id ?
                    { ...r,
                        fields: {
                            ...r.fields,
                            ...e // spread operator here
                        }
                    }: r
            )

        }))
    }
}
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