Created
April 23, 2019 23:43
-
-
Save gaperton/1c64cdd92dd30f6196e3bb83231dd0af to your computer and use it in GitHub Desktop.
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
// Minimal working example demonstrating the two-way data binding with React Hook. | |
import { useLink } from 'valuelink' | |
import * as React from 'react' | |
// We don't want to deal with error markup in every input, do we? | |
const Input = ({ link, ...props }) => { | |
return ( | |
<div> | |
<input {...link.props} {...props} /> | |
<span>{ link.error || '' }</span> | |
</div> | |
) | |
} | |
export const MyCoolComponent = () => { | |
const name = useLink( '' ); | |
// Apply validation rules. | |
name.check( x => x, 'Name is required!' ) | |
.check( x => x.length > 5, "Now it's too short" ) | |
.check( x => x[0] !== 'A', "Oh, and sorry - it can't start with A") | |
.check( x => x[0] !== 'B', "...and with B as well. Sorry for that."); | |
// Markup is not affected. | |
return ( | |
<Input link={name} /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment