Definitely you need to manage the state separately more like option 2. Components are not meant only to be controlled or uncontrolled, you need to consider which information in it should be controlled (via props), and which information should be uncontrolled (via state).
To identify your case, I would suggest follow the official guide thinking-in-react and tic-tac-toe to re-think about your components.
In here I will straight jump to
Step 4: Identify where your state should live
The mindset is
- Identify every component that renders something based on that state.
- Find their closest common parent component—a component above them all in the hierarchy.
- Decide where the state should live
It's very obvious that your child components need the search text and option list to render or change itself while the parent component does not require these values, the parent component only require a list of subcomponents with it's static value(chosen one).
The real-time changes should happened inside, but whenever the value is decided it should lift up the state to parent. That is because from parent perspective, finally it should have a need to collect all values from children, if the state is not stored, then it could became a 'ask' from parent to children which is not a good approach. And it has benefit to implement logic with multiple children. On the other hand, the dropdown data and filter text is useless for parent, they more like kind of 'temp state' better stay inside component, therefore not need to lift them up.
So from my view, I would arrange the state structure like this:
Parent: state: [{type:'',value:''}...]
Parent render children:
for(int i=0; i < state.length; i++){
<Child ...state[i] onValueChanged={(newValue)=> changeState(i,newValue)} />
}
Child props: {type:'',value:'',onValueChanged:(newValue)=>void}
Child state: {data:[], filter:'', focused: false}
The data
of child should be loaded with different given type.
Inside child component the UI is rendered with it's own state.
While the value is chosen, child call onValueChanged
to notify the value changed to parent, so the re-rendering triggered from parent will limited.
Whenever parent need to output the values(like push search text to backend), the state can be directly used.
After all this arrange from component level is done, you could switch the parent state management to reducer with no difficult.
child component with input as well as dropdown
to manageonChange
....notify the parent only onselection
of dropdown element. I will try to reproduce my idea if you could provide a sandbox or something. – Condonation