Last active
May 17, 2019 04:37
-
-
Save gaperton/7ed7c8389c95505e962e663fa6b10edb to your computer and use it in GitHub Desktop.
PuckUser Component
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
function useLink( init ){ | |
const [ value, set ] = useState( init ); | |
// It can be a class with useful methods, like this one: | |
// https://github.com/VoliJS/NestedLink/blob/master/valuelink/src/link.ts | |
// But we just use the plain object here to illustrate an idea. | |
return { value, set }; | |
} | |
const PickUser = ({ $selected /* link to some upper component state */ }) => { | |
// Declare the local component's state as a link. | |
// Now, state elements are easy to distinguish and pass around. | |
const $editing = useLink( false ); | |
return ( | |
<div> | |
{ $editing.value ? | |
<EditUser $selected={$selected /* just pass it through */} | |
close={() => $editing.set( false )}/> | |
: | |
<input value={ userToString( $selected.value ) } | |
onClick={ () => $editing.set( true ) }/> | |
} | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment