react-use-what-input
Version:
React hook for what-input integration
32 lines (29 loc) • 1.1 kB
JavaScript
import { useState, useEffect } from 'react';
import whatInput from 'what-input';
function useWhatInput(intent) {
var _a = useState(whatInput.ask('input')), currentInput = _a[0], setCurrentInput = _a[1];
var _b = useState(whatInput.ask('intent')), currentIntent = _b[0], setCurrentIntent = _b[1];
useEffect(function () {
if (intent !== 'intent') {
setCurrentInput(whatInput.ask('input'));
whatInput.registerOnChange(setCurrentInput, 'input');
}
if (intent !== 'input') {
setCurrentIntent(whatInput.ask('intent'));
whatInput.registerOnChange(setCurrentIntent, 'intent');
}
return function () {
whatInput.unRegisterOnChange(setCurrentInput);
whatInput.unRegisterOnChange(setCurrentIntent);
};
}, [intent]);
switch (intent) {
case 'input':
return currentInput;
case 'intent':
return currentIntent;
default:
return [currentInput, currentIntent];
}
}
export default useWhatInput;