Skip to content

Instantly share code, notes, and snippets.

@jasonmit
Last active April 27, 2019 20:22
Show Gist options
  • Save jasonmit/2c62c7a72832dd47ae3025cac745ea29 to your computer and use it in GitHub Desktop.
Save jasonmit/2c62c7a72832dd47ae3025cac745ea29 to your computer and use it in GitHub Desktop.
const TaskInput = () => {
let shouldPerform = useRef(false);
let [inputValue, setInputValue] = useState('');
let [perform, taskState] = useTask(function*() {
yield timeout(1000);
if (inputValue) {
return [`good ${inputValue}`, `better ${inputValue}`, `best ${inputValue}`];
}
return [];
});
useEffect(() => {
if (shouldPerform.current) {
perform();
}
}, [inputValue]);
let handleInputChange = e => {
shouldPerform.current = true;
setInputValue(e.target.value);
};
return (
<>
<input onChange={handleInputChange} value={inputValue} />
{taskState.lastSuccessful && (
<ul>
{taskState.lastSuccessful.result.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
)}
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment