Download react-instantsearch/examples/default-theme/URLSync.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-instantsearch/examples/default-theme/URLSync.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-instantsearch/examples/default-theme/URLSync.js
-
curl -L -o URLSync.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-instantsearch/examples/default-theme/URLSync.js
1.34 kB
| import React, { Component } from 'react'; | |
| import qs from 'qs'; | |
| const updateAfter = 700; | |
| const searchStateToURL = (searchState) => | |
| searchState ? `${window.location.pathname}?${qs.stringify(searchState)}` : ''; | |
| const withURLSync = (App) => | |
| class WithURLSync extends Component { | |
| state = { | |
| searchState: qs.parse(window.location.search.slice(1)), | |
| }; | |
| componentDidMount() { | |
| window.addEventListener('popstate', this.onPopState); | |
| } | |
| componentWillUnmount() { | |
| clearTimeout(this.debouncedSetState); | |
| window.removeEventListener('popstate', this.onPopState); | |
| } | |
| onPopState = ({ state }) => | |
| this.setState({ | |
| searchState: state || {}, | |
| }); | |
| onSearchStateChange = (searchState) => { | |
| clearTimeout(this.debouncedSetState); | |
| this.debouncedSetState = setTimeout(() => { | |
| window.history.pushState( | |
| searchState, | |
| null, | |
| searchStateToURL(searchState) | |
| ); | |
| }, updateAfter); | |
| this.setState({ searchState }); | |
| }; | |
| render() { | |
| const { searchState } = this.state; | |
| return ( | |
| <App | |
| {...this.props} | |
| searchState={searchState} | |
| onSearchStateChange={this.onSearchStateChange} | |
| createURL={searchStateToURL} | |
| /> | |
| ); | |
| } | |
| }; | |
| export default withURLSync; | |