Skip to content

Instantly share code, notes, and snippets.

@deepakkashyap3013
Last active September 1, 2023 16:31
Show Gist options
  • Select an option

  • Save deepakkashyap3013/d4861c706e844a09025f4a47f7bf38af to your computer and use it in GitHub Desktop.

Select an option

Save deepakkashyap3013/d4861c706e844a09025f4a47f7bf38af to your computer and use it in GitHub Desktop.
import React from "react";
import { withEditableUser } from "./withEditableUser";
// component to be passed as argument to HOC
const FormComponent = ({ user, onChangeUser, onSaveUser, onResetUser }) => {
const { name, age, hairColor } = user || {};
return user ? (
<>
<label>
Name:
<input
value={name}
onChange={(e) => onChangeUser({ name: e.target.value })}
/>
</label>
<label>
Age:
<input
value={age}
onChange={(e) => onChangeUser({ age: Number(e.target.value) })}
/>
</label>
<label>
Hair Color:
<input
value={hairColor}
onChange={(e) => onChangeUser({ hairColor: e.target.value })}
/>
</label>
<button onClick={onResetUser}>Reset</button>
<button onClick={onSaveUser}>Save</button>
</>
) : (
<p>Loading...</p>
);
};
// wrapper to get the userId controllable by the parent component using it
export const editableForm = (userId) => withEditableUser(FormComponent, userId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment