Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Helper function reactjs

I created a helper function to reduce some functions in components. So whenever I need that helper function I could use them in the component needed. I don't know what is the correct way of doing it but I tried this way. I need to pass form, setForm state and event to the helper function. For example.
Main.js

import { useState } from "react";
// helper
import HandleChange from "./HandleChange";

function Main() {
    const [form, setForm] = useState({
        firstName: "",
        lastName: "",
    });

    // ------> This is what I want to move to helper component.
    // function handleChange(event) {
    //   const { name, value } = event.target;

    //   setForm({
    //     ...form,
    //     [name]: value,
    //   });
    // }

    // ------> This is how I used helper component
    const handleChange = HandleChange(form, setForm, event);
    return (
        <input
            type="text"
            name="firstName"
            value={form.firstName}
            {/****** this is how I passed handleChange ******/}
            onChange={handleChange}
        />
    )
}

Helper.js

export default function HandleChange(form, setForm, event) {
  const { name, value } = event.target;

  setForm({
    ...form,
    [name]: value,
  });
}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You just change decalre handleChange like this:

const handleChange = (event) => HandleChange(form, setForm, event);
about 4 years ago · Juan Pablo Isaza Report

0

Interesting approach. I suggest writing the helper function in the following way:


// Component 
[...]
return (
    <input
        type="text"
        name="firstName"
        value={form.firstName}
        onChange={event => setForm(assignValueToName(form, event))}
    />
)

// Helper
function assignValueToName(existing, event) {
    const { name, value } = event.target;
    
    return {...existing, [name]: value};
}
about 4 years ago · Juan Pablo Isaza Report

0

It will be better if you create your custom hook, like this:

This way you can use it directly as any other hook without creating custom logic or passing any parameters

//App.jsx

import useCustomForm from "./myCustomForm";

export default function App() {
  const [form, setForm] = useCustomForm({
    firstName: "qqq",
    lastName: "www"
  });

  return (
    <input
      type="text"
      name="firstName"
      value={form.firstName}
      onChange={setForm}
    />
  );
}


//myCustomForm.js

import { useState } from "react";

export default (defaultState) => {
  const [formState, setFormState] = useState(defaultState);

  const handleChange = ({target}) => {
    const { name, value } = target;
    setFormState({
      ...formState,
      [name]: value
    });
    console.log(formState);
  };

  return [formState, handleChange];
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!