Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is clean ways to handle multiple events in a React component?

I am handling multiple submitting events in parent component when form component submitted, it won’t be a problem for me if I handle only a few events but with multiple events like the code below. I feel it’s not cleans and too much await update(data); be repeated. How can I refactor it code to clean more or is there any other solution?

  function ProfilePage() {

        const { update } = useProfile();

        const fullNameHandler = async (data) => {
            try {
                await update(data);
                toastify('Fullname updated!');
            } catch (err) {
                toastify(err);
            }
        };

        const usernameHandler = async (data) => {
            try {
                await update(data);
                toastify('Username updated!');
            } catch (err) {
                toastify(err);
            }
        };

        const passwordHandler = async (data) => {
            try {
                await update(data);
                toastify('Password updated!');
            } catch (err) {
                toastify(err);
            }
        };

        const phonenumberHandler = async (data) => {
            try {
                await update(data);
                toastify('Phone number updated!');
            } catch (err) {
                toastify(err);
            }
        };

        const emailHandler = async (data) => {
            try {
                await update(data);
                toastify('Email updated!');
            } catch (err) {
                toastify(err);
            }
        };

        const formList = [
            {
                component: <Fullname onSubmit={fullNameHandler} />,
            },
            {
                component: <Username onSubmit={usernameHandler} />,
            },
            {
                component: <Password onSubmit={passwordHandler} />,
            },
            {
                component: <PhoneNumber onSubmit={phonenumberHandler} />,
            },
            {
                component: <Email onSubmit={emailHandler} />,
            },
        ];

        return (
           // ...etc...
        )
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All these functions do that same thing, so you can use a single function to handle all of your inputs. You can also replace the async/await and try/catch with then/catch. Something like this for example:

const handleSubmit = (data, inputName) => {
  update(data)
    .then(() => { toastify(`${inputName} updated!`); })
    .catch(toastify)
};

And then call the function with the inputName parameter:

<Fullname onSubmit={data => handleSubmit(data, "Fullname")} />

Depending on how you return looks like, you can also refactor the form list like this:

const formList = [
  {
    name: "Fullname",
    component: Fullname
  },
  ...
];

And then map:

formList.map(({ name, component: Input }) => (
  <Input onSubmit={data => handleSubmit(data, name)} />
));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading