My project is in SPFX, React/TypeScript/Office UI Fabric. I find that I'm making functions for every text fields for a form I'm making. Is there a function that can handle multiple similar fields, but each text field has it's own unique state.
Example text field function:
private _onReason = (ev: React.FormEvent<HTMLInputElement>, newValue?: string) => {
this.setState({
Reason: newValue
});
}
and another almost identical one:
private _onPurpose = (ev: React.FormEvent<HTMLInputElement>, newValue?: string) => {
this.setState({
Purpose: newValue
});
}
My aim is to allow the user to submit the states of these multiple text fields, but just cut down the code.
I've read this but don't know how to implement into my project: https://www.pluralsight.com/guides/handling-multiple-inputs-with-single-onchange-handler-react
I think the one thing preventing me from implementing that pluralsight solution is the fact I'm using MS fabric UI and the React.FormEvent<HTMLInputElement>, NewValue?: string)
is not liked by the handler function that I attempted below:
public handleChange (evt: React.FormEvent<HTMLInputElement>, newValue?: string) {
const value = evt.target.value;
this.setState({
...this.state,
[evt.target.name]: value
});
}
It doesn't like the value at the end of const value
... it doesn't like the name at the end of [evt.target.name]
either.
UPDATE: After trying something from Peter's link suggestion I can eliminate one of the errors from above, (the value error) but it still gives me the error for [evt.target.name]
(name to be exact).