Skip to content

Commit 375ff72

Browse files
committed
Rewrite examples to use hooks instead of class components
1 parent 6f085dc commit 375ff72

File tree

1 file changed

+57
-80
lines changed

1 file changed

+57
-80
lines changed

example/main.js

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,85 @@
1-
import React from 'react'
1+
import React, { useCallback, useRef, useState } from 'react'
22
import ReactDOM from 'react-dom'
33
import ReactTags from '../lib/ReactTags'
44
import suggestions from './countries'
55

66
/**
77
* Demo 1 - Country selector
88
*/
9-
class CountrySelector extends React.Component {
10-
constructor (props) {
11-
super(props)
129

13-
this.state = {
14-
tags: [
15-
{ id: 184, name: 'Thailand' },
16-
{ id: 86, name: 'India' }
17-
],
18-
suggestions
19-
}
10+
function CountrySelector () {
11+
const [tags, setTags] = useState([])
2012

21-
this.reactTags = React.createRef()
22-
}
13+
const reactTags = useRef()
2314

24-
onDelete (i) {
25-
const tags = this.state.tags.slice(0)
26-
tags.splice(i, 1)
27-
this.setState({ tags })
28-
}
15+
const onDelete = useCallback((tagIndex) => {
16+
setTags(tags.filter((_, i) => i !== tagIndex))
17+
}, [tags])
2918

30-
onAddition (tag) {
31-
const tags = [].concat(this.state.tags, tag)
32-
this.setState({ tags })
33-
}
19+
const onAddition = useCallback((newTag) => {
20+
setTags([...tags, newTag])
21+
}, [tags])
3422

35-
render () {
36-
return (
37-
<>
38-
<p>Select the countries you have visited below:</p>
39-
<ReactTags
40-
ref={this.reactTags}
41-
tags={this.state.tags}
42-
suggestions={this.state.suggestions}
43-
noSuggestionsText='No matching countries'
44-
onDelete={this.onDelete.bind(this)}
45-
onAddition={this.onAddition.bind(this)}
46-
/>
47-
<p><b>Output:</b></p>
48-
<pre><code>{JSON.stringify(this.state.tags, null, 2)}</code></pre>
49-
</>
50-
)
51-
}
23+
return (
24+
<>
25+
<p>Select the countries you have visited below:</p>
26+
<ReactTags
27+
ref={reactTags}
28+
tags={tags}
29+
suggestions={suggestions}
30+
noSuggestionsText='No matching countries'
31+
onDelete={onDelete}
32+
onAddition={onAddition}
33+
/>
34+
<p><b>Output:</b></p>
35+
<pre><code>{JSON.stringify(tags, null, 2)}</code></pre>
36+
</>
37+
)
5238
}
5339

5440
ReactDOM.render(<CountrySelector />, document.getElementById('demo-1'))
5541

5642
/**
5743
* Demo 2 - Custom tags
5844
*/
59-
class CustomTags extends React.Component {
60-
constructor (props) {
61-
super(props)
6245

63-
this.state = {
64-
tags: [],
65-
suggestions: []
66-
}
46+
function CustomTags () {
47+
const [tags, setTags] = useState([])
6748

68-
this.reactTags = React.createRef()
69-
}
49+
const reactTags = useRef()
7050

71-
onDelete (i) {
72-
const tags = this.state.tags.slice(0)
73-
tags.splice(i, 1)
74-
this.setState({ tags })
75-
}
51+
const onDelete = useCallback((tagIndex) => {
52+
setTags(tags.filter((_, i) => i !== tagIndex))
53+
}, [tags])
7654

77-
onAddition (tag) {
78-
const tags = [].concat(this.state.tags, tag)
79-
this.setState({ tags })
80-
}
55+
const onAddition = useCallback((newTag) => {
56+
setTags([...tags, newTag])
57+
}, [tags])
8158

82-
onValidate (tag) {
83-
return /^[a-z]{3,12}$/i.test(tag.name)
84-
}
59+
const onValidate = useCallback((newTag) => {
60+
return /^[a-z]{3,12}$/i.test(newTag.name)
61+
})
8562

86-
render () {
87-
return (
88-
<>
89-
<p>Enter new tags meeting the requirements below:</p>
90-
<ReactTags
91-
allowNew
92-
newTagText='Create new tag:'
93-
ref={this.reactTags}
94-
tags={this.state.tags}
95-
suggestions={this.state.suggestions}
96-
onDelete={this.onDelete.bind(this)}
97-
onAddition={this.onAddition.bind(this)}
98-
onValidate={this.onValidate.bind(this)}
99-
/>
100-
<p style={{ margin: '0.25rem 0', color: 'gray' }}><small><em>Tags must be 3–12 characters in length and only contain the letters A-Z</em></small></p>
101-
<p><b>Output:</b></p>
102-
<pre><code>{JSON.stringify(this.state.tags, null, 2)}</code></pre>
103-
</>
104-
)
105-
}
63+
return (
64+
<>
65+
<p>Enter new tags meeting the requirements below:</p>
66+
<ReactTags
67+
allowNew
68+
newTagText='Create new tag:'
69+
ref={reactTags}
70+
tags={tags}
71+
suggestions={[]}
72+
onDelete={onDelete}
73+
onAddition={onAddition}
74+
onValidate={onValidate}
75+
/>
76+
<p style={{ margin: '0.25rem 0', color: 'gray' }}>
77+
<small><em>Tags must be 3–12 characters in length and only contain the letters A-Z</em></small>
78+
</p>
79+
<p><b>Output:</b></p>
80+
<pre><code>{JSON.stringify(tags, null, 2)}</code></pre>
81+
</>
82+
)
10683
}
10784

10885
ReactDOM.render(<CustomTags />, document.getElementById('demo-2'))

0 commit comments

Comments
 (0)