Skip to content

Commit a8e53ec

Browse files
committed
add new React Demo #18
1 parent e73c221 commit a8e53ec

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,35 @@ Tagger can easily be used with ReactJS.
4141
import { useRef, useState, useEffect } from 'react'
4242
import tagger from '@jcubic/tagger'
4343

44-
function App() {
45-
const [tags, setTags] = useState(null)
46-
const inputRef = useRef(null)
47-
48-
// Get current tags
49-
const getTags = () => {
50-
setTags(inputRef.current.value)
51-
}
52-
53-
// Write the Tagger code inside a useEffect hook
54-
// It will run when the component is initially rendered
55-
useEffect(() => {
56-
// Define the Tagger options
57-
const taggerOptions = {
58-
allow_spaces: true,
59-
}
60-
61-
// Initialize Tagger
62-
tagger(inputRef.current, taggerOptions)
63-
}, [])
64-
65-
return (
66-
<div className='app'>
67-
<input type='text' defaultValue='charles, louis, michel' ref={inputRef} />
68-
<button onClick={getTags}>Get tags</button>
69-
70-
{tags && <pre>{tags}</pre>}
71-
</div>
72-
)
44+
const App = () => {
45+
const [tags, setTags] = useState([]);
46+
const inputRef = useRef(null);
47+
48+
useEffect(() => {
49+
const taggerOptions = {
50+
allow_spaces: true,
51+
};
52+
tagger(inputRef.current, taggerOptions);
53+
onChange();
54+
}, [inputRef]);
55+
56+
const onChange = () => {
57+
setTags(tags_array(inputRef.current.value));
58+
};
59+
60+
return (
61+
<div className="app">
62+
<input type="text" ref={inputRef} onChange={onChange} defaultValue="charles, louis, michel" />
63+
<br/>
64+
<ul>
65+
{tags.map((tag, index) => <li key={`${tag}-${index}`}>{tag}</li>)}
66+
</ul>
67+
</div>
68+
)
69+
}
70+
71+
function tags_array(str) {
72+
return str.split(/\s*,\s*/);
7373
}
7474

7575
export default App

0 commit comments

Comments
 (0)