I'm looking to create a functional component that takes in a hook and its Set function as parameters and constructs an associated input using those fields (along with some other fields). Running into issues with passing in spread operators (as advised in "Learning React, 2nd edition"). Relevant snippets below, help appreciated!
- Generic function that takes in an initial value and returns a new hook and its setValue function:
export const useInput = (initialValue) => {
const [value, setValue] = useState(initialValue);
return [
{ value, onChange: (e) => setValue(e.target.value) },
() => setValue(initialValue),
];
};
- The hook to be used:
const [firstName, setFirstName] = useInput("");
- The functional component (after some cleanup)
const MyInputComp = (props) => {
return (
<div>
<input
className="blah-blah-blah"
{props.obj} // *ERROR*
type="text"
placeholder={props.placeholder}
required={props.required}
/>
</div>
);
};
- Here's how the component is invoked:
<MyInputComp
text="First Name"
obj={...firstName} // *ERROR*
placeholder="Jon"
required={true}
/>