【问题标题】:How to avoid requesting too many HTTP requests on changing Text如何避免在更改文本时请求过多的 HTTP 请求
【发布时间】:2018-02-15 16:09:03
【问题描述】:
  1. 我正在发送关于更改文本的 GET 请求。
  2. 因此,如果我在初始点推荐“my_username”并将用户更改为“hi_user”,我将发送大约 18 个 GET 请求。

请求如下所示

url: ${ROOT_URL}/profile/unamecheck/?un=${username}
body: username
response: {valid: true}

Here is my React Code
_handleChange = async (username) => {
    this.setState({username})
    let response = await axios.get(`${ROOT_URL}/profile/unamecheck/?un=${username}`)
    if (response.status === 200) {
        if(response.data.obtained) {
          this.setState({isValidUsername: false})
        } else {
          this.setState({isValidUsername: true})
        }
    } else {

    }
    console.log('isValidUsername');
    console.log(this.state.isValidUsername);
  }

【问题讨论】:

    标签: django amazon-web-services http servlets react-native


    【解决方案1】:

    为用户的每一次击键发送一个请求是没有用的。存在限制这些请求的库 - 请求仅在用户停止输入时触发。

    debounce plugin 页面有一个关于其实现的演示 - 您可以在浏览器中查看。

    对于 react-native,可以实现来自 answer 的逻辑(它使用 lodash's debounce):

    class MyComponent extends React.Component {
      constructor() {
        this.onChangeTextDelayed = _.debounce(this.onChangeText, 2000);
      }
    
      onChangeText(text) {
        console.log("debouncing");
      }
    
      render() {
        return <Input onChangeText={this.onChangeTextDelayed} />
      }
    }
    

    【讨论】:

    • 这正是我想要的。谢谢
    猜你喜欢
    • 2014-12-01
    • 2014-05-12
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多