Reuse generic component functions
Reuse generic component functions
I'm a very new React programmer and I have no idea how to fix this. In all my forms I have the following code parts.
updateFormData(field, event) {
const { formData } = this.state;
this.setState({ formData: { ...formData, [field]: event } });
}
componentLoaded = (field, data) => {
const { loadedComponents } = this.state;
var fieldName = field + "IsLoaded";
this.setState({ loadedComponents: { ...loadedComponents, [fieldName]: true } });
}
allComponentsLoaded() {
const { loadedComponents } = this.state;
console.log(loadedComponents);
for (var o in loadedComponents) {
if (!loadedComponents[o]) return false;
}
return true;
}
updateFormDateData(field, date) {
var value = date != null && date.isValid() ? date.toISOString() : null;
const { formData } = this.state;
}
When in C# I would put this in a base class. What would be the best way in React to do this?
Oops, made a copy paste error. Will fix that. Thanks
– Patrick
Jun 30 at 23:44
You can create an HOC which just passes those functions down to your component
– st4rl00rd
Jul 1 at 1:08
How can you make a HOC that wraps and a child that then uses those function? For example the all Components loaded function is used in the render method.
– Patrick
Jul 1 at 9:06
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The first three methods are the same thing, just with different names and different variables names(that does the same thing). You could use just one of them
– Matheus Reis
Jun 30 at 23:43