我想使用 lodash debouce
方法来改善客户在输入框中输入内容时的用户体验。出于某种原因,去抖动函数没有从被去抖动的函数返回值。
任何人都可以发现什么可能是错误的吗?
import debounce from "lodash/debounce";
render() {
const inputValid = () => {
console.log('triggered');
return this.state.input.length > 0;
};
let d = debounce(inputValid, 100);
d();
console.log(d()); // this gets logged as undefined although I
//thought it would be either true or false
return <input onChange="updateInput" value={this.state.input} />;
}
最佳答案
debounce 打破同步程序流并使其异步。您不能简单地从中获取返回值。你应该看看这个:
How do I return the response from an asynchronous call?
但是您的示例看起来也不适合该用例。对于确定性渲染方法,只需在没有去抖动处理程序的情况下在流中获取此信息。您应该只根据渲染周期时的状态进行渲染。
debounced 函数应该只用于在 onChange 事件之后触发 setState。
关于javascript - lodash debounce 不触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45588869/