Skip to content

Instantly share code, notes, and snippets.

@gaperton
Last active May 17, 2019 04:37
Show Gist options
  • Save gaperton/7ed7c8389c95505e962e663fa6b10edb to your computer and use it in GitHub Desktop.
Save gaperton/7ed7c8389c95505e962e663fa6b10edb to your computer and use it in GitHub Desktop.
PuckUser Component
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