@@ -41,35 +41,35 @@ Tagger can easily be used with ReactJS.
4141import { useRef , useState , useEffect } from ' react'
4242import 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
7575export default App
0 commit comments