Skip to content

Instantly share code, notes, and snippets.

@ArjanSchouten
Created December 20, 2022 10:13
Show Gist options
  • Save ArjanSchouten/4d784160530c11f9ce824c26c3266637 to your computer and use it in GitHub Desktop.
Save ArjanSchouten/4d784160530c11f9ce824c26c3266637 to your computer and use it in GitHub Desktop.
Two-way databinding for Input elements for React, close to VueJS
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