Skip to content

Instantly share code, notes, and snippets.

@JenniferFuBook
Last active July 25, 2020 22:56
Show Gist options
  • Select an option

  • Save JenniferFuBook/e705c15bcee87a565643b5444d6009d8 to your computer and use it in GitHub Desktop.

Select an option

Save JenniferFuBook/e705c15bcee87a565643b5444d6009d8 to your computer and use it in GitHub Desktop.
const useMyName = initialName => {
const [currentName, setCurrentName] = React.useState(initialName);
return [
newName => setCurrentName(newName),
`My name is ${currentName}.`,
['Jack', 'Larry', 'Tom'].map(item =>[
<label key={item}>
<input
type="radio"
name="names"
data-testid={item}
value={item}
checked={currentName === item}
onChange={() => setCurrentName(item)}
/>
{item}
<br />
</label>,
])
];
}
const App = () => {
const [, message, display] = useMyName('Larry');
return (
<>
{display}
<div>{message}</div>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment