Created
December 20, 2022 10:13
-
-
Save ArjanSchouten/4d784160530c11f9ce824c26c3266637 to your computer and use it in GitHub Desktop.
Two-way databinding for Input elements for React, close to VueJS
This file contains hidden or 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' | |
const bind = (value: string, set: (val: string) => void): Partial<React.InputHTMLAttributes<HTMLInputElement>> => ({ | |
value, | |
onChange: (val) => set(val.currentTarget.value) | |
}) | |
export const MyComponent = () => { | |
const [name, setName] = useState<string>('') | |
return ( | |
<input {...bind(name, setName)} /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment