Skip to content

Instantly share code, notes, and snippets.

@Lahirutech
Created January 29, 2023 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lahirutech/a135d83e17b67b19c4d3d51fa7e3d521 to your computer and use it in GitHub Desktop.
Save Lahirutech/a135d83e17b67b19c4d3d51fa7e3d521 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
function MyComponent() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
// This function is curried and it's a closure because it has access to the setFirstName and setLastName functions
// which are defined in the parent scope
const handleChange = (setter) => (event) => {
setter(event.target.value);
};
return (
<>
<input
type='text'
placeholder='First Name'
onChange={handleChange(setFirstName)}
/>
<input
type='text'
placeholder='Last Name'
onChange={handleChange(setLastName)}
/>
<p>
Name: {firstName} {lastName}
</p>
</>
);
}
export default MyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment