Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda