Last active
April 27, 2019 20:22
-
-
Save jasonmit/2c62c7a72832dd47ae3025cac745ea29 to your computer and use it in GitHub Desktop.
This file contains 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
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