反应中的输入框以显示反应中的建议
Input box in react to show suggestions in reactjs
如何制作一个输入框,它会以小的延迟给出建议,而不是在每个字符输入时都很快。我不想在每个字符输入上都点击建议 api。
class Input extends React.Component {
constructor (props){
super(props);
this.state = {
inputVal:'',
suggestion : []
}
}
handleChange = ({target:{value}})=>{
fetch('https://api.github.com/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.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container'></div>
您可以使用延迟的 API 调用,使用每次更改输入值后都会清除的 setTimeout。这是一个小的工作示例:
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.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
如何制作一个输入框,它会以小的延迟给出建议,而不是在每个字符输入时都很快。我不想在每个字符输入上都点击建议 api。
class Input extends React.Component {
constructor (props){
super(props);
this.state = {
inputVal:'',
suggestion : []
}
}
handleChange = ({target:{value}})=>{
fetch('https://api.github.com/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.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container'></div>
您可以使用延迟的 API 调用,使用每次更改输入值后都会清除的 setTimeout。这是一个小的工作示例:
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.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>