Correct way to throttle HTTP calls based on state in redux and react
This is the simplest approach. When the input triggers a change, it calls a debounced version of setSearch delaying the server call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import * as React from "react" import {connect} from "react-redux" import {setSearch} from "./actions" export default connect( null, function mapDispatchToProps(dispatch) { const setSearch_ = _.debounce(q => dispatch(setSearch(q)), 1000) return () => ({setSearch: setSearch_}) } )( function SearchForm(props) { const {setSearch} = props return ( <input type="search" onChange={setSearch} /> ) } ) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.