Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!