Skip to content

Commit 0f9c691

Browse files
docs(SearchExampleStandard): use React hooks (#3794)
* docs(SearchExampleStandard): your description Converted into Functional functional component * moved source out of function * Update SearchExampleStandard.js * Update SearchExampleStandard.js * Update SearchExampleStandard.js Co-authored-by: Oleksandr Fediashov <[email protected]>
1 parent 91c9040 commit 0f9c691

File tree

1 file changed

+72
-44
lines changed

1 file changed

+72
-44
lines changed
Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,95 @@
11
import _ from 'lodash'
22
import faker from 'faker'
3-
import React, { Component } from 'react'
3+
import React from 'react'
44
import { Search, Grid, Header, Segment } from 'semantic-ui-react'
55

6-
const initialState = { isLoading: false, results: [], value: '' }
7-
86
const source = _.times(5, () => ({
97
title: faker.company.companyName(),
108
description: faker.company.catchPhrase(),
119
image: faker.internet.avatar(),
1210
price: faker.finance.amount(0, 100, 2, '$'),
1311
}))
1412

15-
export default class SearchExampleStandard extends Component {
16-
state = initialState
13+
const initialState = {
14+
loading: false,
15+
results: [],
16+
value: '',
17+
}
18+
19+
function exampleReducer(state, action) {
20+
switch (action.type) {
21+
case 'CLEAN_QUERY':
22+
return initialState
23+
case 'START_SEARCH':
24+
return { ...state, loading: true, value: action.query }
25+
case 'FINISH_SEARCH':
26+
return { ...state, loading: false, results: action.results }
27+
case 'UPDATE_SELECTION':
28+
return { ...state, value: action.selection }
1729

18-
handleResultSelect = (e, { result }) => this.setState({ value: result.title })
30+
default:
31+
throw new Error()
32+
}
33+
}
1934

20-
handleSearchChange = (e, { value }) => {
21-
this.setState({ isLoading: true, value })
35+
function SearchExampleStandard() {
36+
const [state, dispatch] = React.useReducer(exampleReducer, initialState)
37+
const { loading, results, value } = state
2238

23-
setTimeout(() => {
24-
if (this.state.value.length < 1) return this.setState(initialState)
39+
const timeoutRef = React.useRef()
40+
const handleSearchChange = React.useCallback((e, data) => {
41+
clearTimeout(timeoutRef.current)
42+
dispatch({ type: 'START_SEARCH', query: data.value })
2543

26-
const re = new RegExp(_.escapeRegExp(this.state.value), 'i')
44+
timeoutRef.current = setTimeout(() => {
45+
if (data.value.length === 0) {
46+
dispatch({ type: 'CLEAN_QUERY' })
47+
return
48+
}
49+
50+
const re = new RegExp(_.escapeRegExp(data.value), 'i')
2751
const isMatch = (result) => re.test(result.title)
2852

29-
this.setState({
30-
isLoading: false,
53+
dispatch({
54+
type: 'FINISH_SEARCH',
3155
results: _.filter(source, isMatch),
3256
})
3357
}, 300)
34-
}
58+
}, [])
59+
React.useEffect(() => {
60+
return () => {
61+
clearTimeout(timeoutRef.current)
62+
}
63+
}, [])
3564

36-
render() {
37-
const { isLoading, value, results } = this.state
65+
return (
66+
<Grid>
67+
<Grid.Column width={6}>
68+
<Search
69+
loading={loading}
70+
onResultSelect={(e, data) =>
71+
dispatch({ type: 'UPDATE_SELECTION', selection: data.result.title })
72+
}
73+
onSearchChange={handleSearchChange}
74+
results={results}
75+
value={value}
76+
/>
77+
</Grid.Column>
3878

39-
return (
40-
<Grid>
41-
<Grid.Column width={6}>
42-
<Search
43-
loading={isLoading}
44-
onResultSelect={this.handleResultSelect}
45-
onSearchChange={_.debounce(this.handleSearchChange, 500, {
46-
leading: true,
47-
})}
48-
results={results}
49-
value={value}
50-
/>
51-
</Grid.Column>
52-
<Grid.Column width={10}>
53-
<Segment>
54-
<Header>State</Header>
55-
<pre style={{ overflowX: 'auto' }}>
56-
{JSON.stringify(this.state, null, 2)}
57-
</pre>
58-
<Header>Options</Header>
59-
<pre style={{ overflowX: 'auto' }}>
60-
{JSON.stringify(source, null, 2)}
61-
</pre>
62-
</Segment>
63-
</Grid.Column>
64-
</Grid>
65-
)
66-
}
79+
<Grid.Column width={10}>
80+
<Segment>
81+
<Header>State</Header>
82+
<pre style={{ overflowX: 'auto' }}>
83+
{JSON.stringify({ loading, results, value }, null, 2)}
84+
</pre>
85+
<Header>Options</Header>
86+
<pre style={{ overflowX: 'auto' }}>
87+
{JSON.stringify(source, null, 2)}
88+
</pre>
89+
</Segment>
90+
</Grid.Column>
91+
</Grid>
92+
)
6793
}
94+
95+
export default SearchExampleStandard

0 commit comments

Comments
 (0)