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

109
Visualizações
How do I update deeply nested array with hooks in react?

I have a nested array of objects, each object have a nested options array like this.

const [formFields, setFormFields ] = useState({
    formTitle: '',
    fields: [
        {name: 'country', val: '', type: 'radio', options: ['Japan', 'Korea', 'usa'] },
        {name: 'state', val: '', type: 'select', options: ['texas', 'florida']},
    {name: 'location', val: '', type: 'text', options: []},
        
    ]})

Each of the items in the nested options array is supposed to be a value in a textInput which is editable. I want to be able to add/remove/edit these values inside the textInput with a button click. Please how will I be able to achieve this?

my code

 <Containter>
        {formFields.fields.map((field, index) => (
            <View key={index}>
          <View>
            <TextInput
                onChangeText={(value ) => {
                    onChange({name: field.name, value });
                    }}
                value={field.name}
            />

          </View>
              
            {(field.type === 'select' || field.type === 'radio') && (
                <>
                    {field.options.map((option) => (
                        <TextInput value={option} 
                        onChangeText={(value ) => {
                            onChange({name: field.options, ...field.options, value });
                            }}
                        
                        />
        <Text onPress={removeOption}>X</Text>
                    ))}

                    <Button title="add option" />
                </>
            )
            }
            <IconButton
                icon="delete"
                onPress={handleRemoveField}
            />

                
            </View>

        ))}
                <Button
                    onPress={handleAddField}
                    title="Add"
            
                />

               
    </Containter>
about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Add & remove implementation:

onAdd (index,value) {
    const fields = formFields.fields.map((field,i) => { 
        if (i==index) {
           const options = [...field.options,value]
           return {...field, options}
        }
        return field
    })
    setFormFields(
        {
            ...formFields,
            fields
        }
    )
}
onRemove (index,value) {
    const fields = formFields.fields.map((field,i) => { 
        if (i==index) {
            const options = field.options.filter((item) => item != value)
            return {...field, options}
        }
        return field
    })
    setFormFields(
        {
            ...formFields,
            fields
        }
    )
}

about 4 years ago · Santiago Gelvez Relatório

0

// in constructor
this.onChange = this.onChange.bind(this)

// in class
onChange (index,value) {
    this.setState(state => {
      const fields = state.fields.map((field,i) => { 
          if (i==index) field.val = value
          return field
      })
      return {
          ...state,
          fields
      }
    })
}


// in component 
onChangeText( (e) => onChange(index, e.target.value) )

about 4 years ago · Santiago Gelvez Relatório

0

For value changing:

onChange (index,value) {
  const fields = formFields.fields.map((field,i) => { 
    if (i==index) field.val = value
    return field
  })
  setFormFields({
    ...formFields,
    fields
  })
}
...
// somewhere in input element
<TextInput ... onChangeText={(e) => onChange(index,e.target.value)} .. />

about 4 years ago · Santiago Gelvez 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