简体   繁体   English

如何在 React 中正确地自动完成

[英]How to make correctly autocomplete in React

I want to make autocomplete input of cities in React.我想在 React 中自动完成城市输入。 I have json file of cities ~15mb.我有json城市文件 ~15mb。 How should I correctly implement it?我应该如何正确实施它? Should I keep this json on client side?我应该把这个json放在客户端吗?

you could make a request to the server on every keystroke but start the search after say 3 characters您可以在每次击键时向服务器发出请求,但在说 3 个字符后开始搜索

eg searching for London例如搜索伦敦

You would only make the first request after the user has typed in Lon您只会在用户输入Lon后发出第一个请求

15mb seems a lot though, maybe you want to think of filtering this down beforehand so you request less countries back.不过 15mb 似乎很多,也许您想考虑事先过滤掉它,以便您请求更少的国家/地区。 eg make the user select a country beforehand so you only request cities in that country.例如,预先将用户 select 设为国家/地区,因此您只需请求该国家/地区的城市。 so if they pick USA, then type in Chi it would only return cities in the USA starting with Chi因此,如果他们选择 USA,则输入Chi它只会返回以Chi开头的USA城市

I don't think it's a good idea to pull the 15mb client side as that is a huge amount of data我不认为拉 15mb 客户端是一个好主意,因为那是大量的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM