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

128
Visualizações
How to overcome the state overwriting in the bellow sample in React?

When there is no field.value for the existing product context works fine and updates the product name from nested dropdown correctly.

Context looks like this:

const NamingContext = React.createContext({
    name: '',
    setName: () => {}
});

The parent component looks like this:

const [name, setName] = useState('');
const value = { name, setName };
return (
    <Naming.Provider value={value}>
                <span>Product name</span>
                <Controller
                    name="Name"
                    control={props.control}
                    render={(field) => {
                        if (field.value) {
                            setName(field.name);
                        }
                        return (
                            <Input
                                value={value.name}
                                onChange={(e) =>
                                    field.onChange(e.target.value)
                                }
                            />
                        );
                    }}
                />

So for the blank editor when I choose from deeply nested child dropdown an available name it updates the context and shows the value in the above name input. However if there is a field.value it always overwrites newly selected value from the context. What is the smartest way to track the latest update in the above component. So if there is a field.value and then you select a new item from dropdown it will show the new value from the context and will not be overwritten by field.value again?

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