replace class components (HOCs) with React Hooks

This commit is contained in:
proddy
2021-09-20 21:43:56 +02:00
parent 09d8a6360b
commit a31cf53863
10 changed files with 214 additions and 69 deletions

View File

@@ -0,0 +1,33 @@
type UpdateEntity<S> = (state: (prevState: Readonly<S>) => S) => void;
export const extractEventValue = (
event: React.ChangeEvent<HTMLInputElement>
) => {
switch (event.target.type) {
case 'number':
return event.target.valueAsNumber;
case 'checkbox':
return event.target.checked;
default:
return event.target.value;
}
};
export const updateValue = <S>(updateEntity: UpdateEntity<S>) => (
event: React.ChangeEvent<HTMLInputElement>
) => {
updateEntity((prevState) => ({
...prevState,
[event.target.name]: extractEventValue(event)
}));
};
export const updateBooleanValue = <S>(updateEntity: UpdateEntity<S>) => (
name: string,
value?: boolean
) => {
updateEntity((prevState) => ({
...prevState,
[name]: value
}));
};