Last active
December 20, 2022 21:14
-
-
Save elijahmanor/08fc6c8468c994c844213e4a4344a709 to your computer and use it in GitHub Desktop.
React Debouncing Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { render } from "react-dom"; | |
import "./index.css"; | |
class Widget extends Component { | |
state = { text: "" }; | |
handleChange = (e) => { | |
this.setState({ text: e.target.value }); | |
}; | |
render() { | |
return ( | |
<div> | |
<input onChange={this.handleChange} /> | |
<textarea value={this.state.text} /> | |
</div> | |
); | |
} | |
} | |
class App extends Component { | |
state = { show: true }; | |
handleToggle = () => { | |
this.setState(prevState => ({ show: !prevState.show })); | |
}; | |
render() { | |
return ( | |
<div> | |
<button onClick={this.handleToggle}>Toggle</button> | |
{this.state.show ? <Widget /> : null} | |
</div> | |
); | |
} | |
} | |
render(<App />, document.getElementById("⚛️")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { render } from "react-dom"; | |
import { debounce } from "lodash"; | |
import "./index.css"; | |
class Widget extends Component { | |
state = { text: "" }; | |
handleChange = (e) => { | |
this.setState({ text: e.target.value }); | |
}; | |
render() { | |
return ( | |
<div> | |
<input onChange={this.handleChange} /> | |
<textarea value={this.state.text} /> | |
</div> | |
); | |
} | |
} | |
class App extends Component { | |
state = { show: true }; | |
handleToggle = debounce(() => { | |
this.setState(prevState => ({ show: !prevState.show })); | |
}, 1000); | |
render() { | |
return ( | |
<div> | |
<button onClick={this.handleToggle}>Toggle</button> | |
{this.state.show ? <Widget /> : null} | |
</div> | |
); | |
} | |
} | |
render(<App />, document.getElementById("⚛️")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { render } from "react-dom"; | |
import { debounce } from "lodash"; | |
import "./index.css"; | |
class Widget extends Component { | |
state = { text: "" }; | |
handleChange = debounce((text) => { | |
this.setState({ text }); | |
}, 500); | |
componentWillUmount() { | |
this.handleChange.cancel(); | |
} | |
render() { | |
return ( | |
<div> | |
<input onChange={e => this.handleChange(e.target.value)} /> | |
<textarea value={this.state.text} /> | |
</div> | |
); | |
} | |
} | |
class App extends Component { | |
state = { show: true }; | |
handleToggle = () => { | |
this.setState(prevState => ({ show: !prevState.show })); | |
}; | |
render() { | |
return ( | |
<div> | |
<button onClick={this.handleToggle}>Toggle</button> | |
{this.state.show ? <Widget /> : null} | |
</div> | |
); | |
} | |
} | |
render(<App />, document.getElementById("⚛️")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { render } from "react-dom"; | |
import { debounce } from "lodash"; | |
import "./index.css"; | |
class Widget extends Component { | |
state = { text: "" }; | |
debounceEvent(...args) { | |
this.debouncedEvent = debounce(...args); | |
return e => { | |
e.persist(); | |
return this.debouncedEvent(e); | |
}; | |
} | |
handleChange = (e) => { | |
this.setState({ text: e.target.value }); | |
}; | |
componentWillUmount() { | |
this.debouncedEvent.cancel(); | |
} | |
render() { | |
return ( | |
<div> | |
<input onChange={this.debounceEvent(this.handleChange, 500)} /> | |
<textarea value={this.state.text} /> | |
</div> | |
); | |
} | |
} | |
class App extends Component { | |
state = { show: true }; | |
handleToggle = () => { | |
this.setState(prevState => ({ show: !prevState.show })); | |
}; | |
render() { | |
return ( | |
<div> | |
<button onClick={this.handleToggle}>Toggle</button> | |
{this.state.show ? <Widget /> : null} | |
</div> | |
); | |
} | |
} | |
render(<App />, document.getElementById("⚛️")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo in
componentWillUmount
=>componentWillUnmount
👍https://gist.github.com/elijahmanor/08fc6c8468c994c844213e4a4344a709#file-index-2-event-obj-debounce-a-js-L11
https://gist.github.com/elijahmanor/08fc6c8468c994c844213e4a4344a709#file-index-3-event-obj-debounce-b-js-L18