首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hyperapp中输入元素输入事件的去弹动作

Hyperapp中输入元素输入事件的去弹动作
EN

Stack Overflow用户
提问于 2018-12-19 10:04:15
回答 1查看 137关注 0票数 3

当用户在搜索字段中输入时,我正在尝试(使用lodash.debounce)在Hyperapp组件中删除一个操作。虽然动作延迟的,但它们似乎都是排队的,并且在间隔(500 ms)过去时都会单独执行,而不是完全跳过。

换句话说:如果用户在500 ms内键入"foo“,它将执行三次,每个函数调用延迟500 ms,而不是只执行一次。

我在非Hyperapp环境中使用了很多次,所以感觉好像我错过了Hyperapp的工作方式。

代码语言:javascript
复制
export default ({ state, actions }) => {
  const debouncedFetchResults = debounce(actions.fetchResults, 500);

  return (
    <input
      type="search"
      name="search"
      value={state.searchPhrase}
      oninput={
        (e) => {
          // Set `state.searchPhrase`
          actions.setSearchPhrase(e.target.value);

          // Search using `state.searchPhrase`
          debouncedFetchResults();
        }
      } />
  );
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-02 08:16:02

最后,我使用以下方法修复了这个问题:

代码语言:javascript
复制
const debouncedFetchResults = debounce(fn => fn(), 500);

export default ({ state, actions }) => (
  <input
    type="search"
    name="search"
    value={state.searchPhrase}
    oninput={
      (e) => {
        // Set `state.searchPhrase`
        actions.setSearchPhrase(e.target.value);

        // Search using `state.searchPhrase`
        debouncedFetchResults(actions.fetchResults);
      }
    } />
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53848818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档