Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created May 17, 2020 18:02
Show Gist options
  • Save jsmanifest/bb8ca849f3b20387cd907af8ad7f8de0 to your computer and use it in GitHub Desktop.
Save jsmanifest/bb8ca849f3b20387cd907af8ad7f8de0 to your computer and use it in GitHub Desktop.
function MyInput({ value, onChange: onChangeProp }) {
const ref = React.useRef()
function onChange(e) {
if (isDigits(e.target.value) && isWithin6(e.target.value)) {
onChangeProp(e, { ref: ref.current })
}
}
return (
<div>
<input ref={ref} type='text' value={value} onChange={onChange} />
</div>
)
}
function App() {
const [value, setValue] = React.useState('')
function onChange(e, { ref }) {
setValue(e.target.value)
if (ref.type === 'file') {
// It's a file input
} else if (ref.type === 'text') {
// Do something
}
}
return (
<div>
<MyInput value={value} onChange={onChange} />
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment