I am using react hooks useState to update an object. I have a reusable component, which is just an input, that I am passing props to and an onChange handler that updates the state.
How can i use the prop value "fooType" to dynamically update the state? This is working other than fooType
in fooType: e.target.value` not being recognized as a variable. Is there a way to refactor for this to work in this manner?
The input component:
const Input = (props) => {
const { foo, foos, setFoos, fooType } = props;
return (
<input type='text'
placeholder={ foo }
value={ foo }
onChange={ (e) => setFoos({ ...foos, fooType: e.target.value }) } />
);
};
Here is the file where I am passing the props to three inputs
const InputGroup = (props) => {
// initial state
const [foos, setFoos] = useState({
foo1: 0, foo2: 0, foo3: 0
});
return (
<div>
<Input foo={ foos.foo1 }
fooType='foo1'
foos={ foos }
setFoos={ setFoos } />
<Input foo={ foos.foo2 }
fooType='foo2'
foos={ foos }
setFoos={ setFoos } />
<Input foo={ foos.foo3 }
fooType='foo3'
foos={ foos }
setFoos={ setFoos }/>
</div>
);
}