Created
January 29, 2023 10:23
-
-
Save Lahirutech/a135d83e17b67b19c4d3d51fa7e3d521 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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