score:2
Accepted answer
const handleChange = (event) => HandleChange(form, setForm, event);
score:1
// 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};
}
score:1
//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];
};
Source: stackoverflow.com
Related Query
- Helper function reactjs
- ReactJS lifecycle method inside a function Component
- "this" is undefined inside map function Reactjs
- expected assignment or function call: no-unused-expressions ReactJS
- What will happen if I use setState() function in constructor of a Class in ReactJS or React Native?
- ReactJs CreateClass is not a function
- Passing a function with parameters through props on reactjs
- ReactJs Global Helper Functions
- TypeError: Object(...) is not a function reactjs
- setTimeout ReactJS with arrow function es6
- ReactJS function isn't getting latest state
- ReactJS with ES6: this.props is not a function when I communicate two components
- Function inside render and class in reactjs
- How to pass a function as an argument to a ReactJS component in TypeScript
- ReactJS giving inst.render is not a function error
- How to pass form values as FormData in reactjs on submit function
- Reactjs setState arrow function syntax
- ReactJS component PropTypes - specify a function type with a set number parameters
- ReactJS toLowerCase is not a function
- ReactJS Test - fsevents is not a function
- ReactJS writing stateless function comments
- ReactJS - Need to click twice to set State and run function
- ReactJS Array.push function not working in setState
- How to pass in a second argument on ReactJS onSubmit function call
- Simple function to Validate checkboxes in reactJS
- How to map inside a map function in reactjs
- Function listed for the onClick event is firing at load time on ReactJS
- Understanding css helper function in styled components
- Reactjs this.setState is not a function error
- ReactJS defaultProps empty function declaration
More Query from same tag
- ReactJS: Help debugging? Sum of each array NOT columns
- use lodash for iterating the array and filter
- onClick on <li> does't trigger
- Looping thru nested array, and also being able to search/filter it
- firebase auth.sendSignInLinkToEmail method is not working
- React.js: How to redirect to another page upon click of menu?
- How can I invoke a callback within a React function component?
- Mapping over object nested in an object coming from API | JSX Mapping problem
- Using Buttons in React Material UI Table
- React: Render a single quote from json file
- Hover over div/image to fade out image and have text fade in, but hovering over text removes fade out of image. How to prevent?
- Getting Uncaught TypeError: onKeyPress is not a function in React typescript functional component
- dynamic loading of react components from string name
- Cannot upgrade to React-Bootstrap latest version
- How to prevent redux-form Field events from triggering state check in parent
- React router: how to pass param from URL into component?
- NoUiSlider not visible
- How to pass import as props parameters in React JS
- Why is PropTypes throwing error in an internal function of a functional component?
- Getting error Invariant Violation tried to get frame out of range index?
- Redux-thunk dispatch function doesn't work on Laravel
- Render after componentWillReceiveProps and before componentDidUpdate
- disable all click synthesis events of react
- Using nth-child css on dynamic React table
- Next.js: Links inside dropdown menu not working
- Automatically going to a div element created in React Virtual Dom
- React-URLSearchParams returning Null object
- How to implement side effect on observable property?
- props not being updated properly in Login page button action with React JS
- CombineReducers with Typescript returns error "Argument of type is not assignable to parameter of type 'ReducersMapObject'"