($width) AND $width .= 'px'; $style = " style=\"width: $width\""; } $value = $value ? $value : date('H:i'); $s = ""; return $s; } // form_date('start', '2018-07-05') 为空则当前日期 function form_date($name, $value = 0, $width = FALSE) { $style = ''; if (FALSE !== $width) { is_numeric($width) AND $width .= 'px'; $style = " style=\"width: $width\""; } $value = $value ? $value : date('Y-m-d'); $s = ""; return $s; } /**用法 * * echo form_radio_yes_no('radio1', 0); * echo form_checkbox('aaa', array('无', '有'), 0); * * echo form_radio_yes_no('aaa', 0); * echo form_radio('aaa', array('无', '有'), 0); * echo form_radio('aaa', array('a'=>'aaa', 'b'=>'bbb', 'c'=>'ccc', ), 'b'); * * echo form_select('aaa', array('a'=>'aaa', 'b'=>'bbb', 'c'=>'ccc', ), 'a'); */ ?>javascript - Input box in react to show suggestions in reactjs - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Input box in react to show suggestions in reactjs - Stack Overflow

programmeradmin3浏览0评论

How to make an input box which will give suggestion with small delay not on every character input fast. I don't want to hit suggestion api on every char input.

class Input extends React.Component {
constructor (props){
super(props);
this.state = {
  inputVal:'',
  suggestion : []
}
}

handleChange = ({target:{value}})=>{
  fetch('='+value, (res)=>{
  this.setState({suggestions:res.items});
  });  
}


  render(){
    <input onChange={this.handleChange} value {this.state.inputVal} />
    <ul id="suggestions">
    this.state.suggestions.map(sugg=>{
      return (
      <li>{sugg.login}</li>
      )
    })
    </ul>
  }
}

ReactDOM.render(<Input />, document.getElementById('container'));
<script src=".1.0/react.min.js"></script>
<script src=".1.0/react-dom.min.js"></script>
<div id='container'></div>

How to make an input box which will give suggestion with small delay not on every character input fast. I don't want to hit suggestion api on every char input.

class Input extends React.Component {
constructor (props){
super(props);
this.state = {
  inputVal:'',
  suggestion : []
}
}

handleChange = ({target:{value}})=>{
  fetch('https://api.github./search/users?q='+value, (res)=>{
  this.setState({suggestions:res.items});
  });  
}


  render(){
    <input onChange={this.handleChange} value {this.state.inputVal} />
    <ul id="suggestions">
    this.state.suggestions.map(sugg=>{
      return (
      <li>{sugg.login}</li>
      )
    })
    </ul>
  }
}

ReactDOM.render(<Input />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container'></div>

Share Improve this question edited Aug 2, 2018 at 11:21 Rizvan asked Aug 2, 2018 at 11:00 RizvanRizvan 5511 gold badge7 silver badges20 bronze badges 2
  • 1 What have you tried so far? Add your code to get more answers. – jsalonen Commented Aug 2, 2018 at 11:02
  • @jsalonen I have added the snippet for viewing my progress, but I could not make it work here as I don't know how run react here – Rizvan Commented Aug 2, 2018 at 11:22
Add a ment  | 

1 Answer 1

Reset to default 7

You can use delayed API call using setTimeout that is cleared after each change of input value. Here's a small working example:

const INPUT_TIMEOUT = 250; //ms - It's our input delay
class TodoApp extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        value: '',
        predictions: [],
      };

      this.onChange = this.onChange.bind(this);
    }

    getPredictions(value) {
      // let's say that it's an API call
      return [
        'Boston',
        'Los Angeles',
        'San Diego',
        'San Franciso',
        'Sacramento',
        'New York',
        'New Jersie',
        'Chicago',
      ].filter(item => item.toLowerCase().indexOf(value.toLowerCase()) !== -1);
    }

    onChange(e) {
      // clear timeout when input changes value
      clearTimeout(this.timeout);
      const value = e.target.value;
      this.setState({
        value
      });

      if (value.length > 0) {
        // make delayed api call
        this.timeout = setTimeout(() => {
          const predictions = this.getPredictions(value);
          this.setState({
            predictions
          });
        }, INPUT_TIMEOUT);
      } else {
        this.setState({
          predictions: []
        });
      }
    }

    render() {
        return ( 
          <div >
          <input type = "text" value={this.state.value} onChange = {this.onChange}/>
            <div> 
            {
              this.state.predictions.map((item, index) => (
                <div key={index + item}>{item}</div>
              ))
            } 
            </div> 
          </div>
        )
    }
}

ReactDOM.render( <TodoApp />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="app"></div>

发布评论