Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start search only when user stops typing?

I need to perform a Search when user stops typing.I know I am supposed to use setTimeout() . But with Reactjs I cant find how it works. Can someone please tell me how to invoke a method (that will handle Search) when the user stops typing for a few seconds (suppose 5).I cant figure out where to write the code to check that the user has stopped typing.

import React, {Component, PropTypes} from 'react';  export default class SearchBox extends Component {      state={       name:" ",     }      changeName = (event) => {         this.setState({name: event.target.value});      }      sendToParent = () => {         this.props.searching(this.state.name);     }      render() {         return (             <div>                  <input type="text"  placeholder='Enter name you wish to Search.'  onChange={this.changeName} />              </div>         );     } }    

I want to invoke the sendToParent method when the user stops typing.

like image 228
shinite Avatar asked Feb 14 '17 03:02

shinite


People also ask

How do you execute a function only after the user stops typing?

Executing a function after a certain amount of inactivity is known as debouncing. Debouncing can be helpful for many reasons. One of the most popular applications in web development is to execute a search or filter some results after a user has stopped typing text in a textbox.

How do you set setTimeout in React?

How to use setTimeout in React. The setTimeout function accepts two arguments: the first is the callback function that we want to execute, and the second specifies the timeout in milliseconds before the function will be called. setTimeout(() => console. log('Initial timeout!'

What is onBlur React native?

What is onBlur event in React. React onBlur behaves just like the native JavaScript version of blur. Every time you get out of focus from the input field, the event will trigger. It doesn't matter if the value has changed or not, every time you get out of focus.


2 Answers

You can use setTimeout with respect to your code as follows,

state = {     name: '',     typing: false,     typingTimeout: 0 } changeName = (event) => {     const self = this;      if (self.state.typingTimeout) {        clearTimeout(self.state.typingTimeout);     }      self.setState({        name: event.target.value,        typing: false,        typingTimeout: setTimeout(function () {            self.sendToParent(self.state.name);          }, 5000)     }); } 

Also, you need to bind changeName handler function in constructor.

constructor(props) {    super(props);    this.changeName = this.changeName.bind(this); } 
like image 175
Saba Hassan Avatar answered Sep 16 '22 16:09

Saba Hassan


Implement using useEffect hook:

function Search() {   const [searchTerm, setSearchTerm] = useState('')    useEffect(() => {     const delayDebounceFn = setTimeout(() => {       console.log(searchTerm)       // Send Axios request here     }, 3000)      return () => clearTimeout(delayDebounceFn)   }, [searchTerm])    return (     <input       autoFocus       type='text'       autoComplete='off'       className='live-search-field'       placeholder='Search here...'       onChange={(e) => setSearchTerm(e.target.value)}     />   ) } 
like image 45
Harshal Avatar answered Sep 19 '22 16:09

Harshal