gpt4 book ai didi

javascript - 用户输入几秒钟后查询搜索 Meteor + React

转载 作者:行者123 更新时间:2023-12-03 05:26:32 25 4
gpt4 key购买 nike

我正在实现一个搜索组件,我只想在用户输入几秒钟后执行该组件,因此我不会在每个键输入上查询数据库。不确定在哪个 block 中执行 _.debouncesetTimeout

我正在使用react-komposer,它看起来像这样

import { composeWithTracker } from 'react-komposer';
import { Ruuts } from '../../api/ruuts/ruuts.js';
import { RuutsList } from '../components/ruuts-list.js';
import { Loading } from '../components/loading.js';
import { Meteor } from 'meteor/meteor';

const composer = (params, onData) => {
let search = getParameterByName('s');
if (search == null) {
search = "";
}
const subscription = Meteor.subscribe('ruuts', search);
if (subscription.ready()) {
const ruuts = Ruuts.find({ ownerId: Meteor.userId() }, { sort: { title: 1 } }).fetch();
onData(null, { ruuts });
}
};

export default composeWithTracker(composer, Loading)(RuutsList);

function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}

这是搜索组件

import React from 'react';
import { FormGroup, FormControl } from 'react-bootstrap';

export const SearchRuuts = React.createClass({
handleSearch(e) {
this.props.router.push({
...this.props.location,
query: {s: e.target.value}
});
},

render() {
return <FormGroup className="search-ruut">
<FormControl
type="text"
ref="searchRuut"
onKeyUp={this.handleSearch}
placeholder="Search by Requisition Number or Title"
/>
</FormGroup>
}
});

最佳答案

您可以将普通的 onKeyUp 处理程序包装在 _.debounce 中:

onKeyUp={_.debounce(this.handleSearch, 500)}

关于javascript - 用户输入几秒钟后查询搜索 Meteor + React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41126695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com