id is not definedというエラーが出てしまい、登録したものを削除できなく困っています。
onClickDeleteの箇所でエラーが出ます。
いろいろ調べましたが、idの紐つけ方がわかりません。
よろしくお願いいたします。
以下全体コードになります。
react
1import React, { useState, useEffect } from 'react'; 2import { makeStyles } from '@material-ui/core/styles'; 3import { TextField, List, ListItem, Checkbox, Button } from '@material-ui/core'; 4import AudiotrackIcon from '@material-ui/icons/Audiotrack'; 5import AddBox from '@material-ui/icons/Add'; 6import DeleteForeverIcon from '@material-ui/icons/DeleteForever'; 7import { v4 as uuidv4 } from 'uuid'; 8import { db } from '../../firebase'; 9import firebase from 'firebase'; 10 11const useStyles = makeStyles({ 12 root: { 13 width: '100%', 14 }, 15 modal: { 16 display: 'flex', 17 alignItems: 'center', 18 justifyContent: 'center', 19 }, 20 paper: { 21 backgroundColor: 'white', 22 border: '2px solid #000', 23 boxShadow: 5, 24 padding: 3 25 }, 26 btn: { 27 width: '100%' 28 }, 29 main: { 30 width: '70%', 31 height: '70%' 32 }, 33 list: { 34 width: '80%', 35 padding: 0, 36 backgroundColor: 'white', 37 border: '1px solid black' 38 }, 39 container: { 40 display: 'flex', 41 }, 42 item: { 43 borderBottom: '1px solid black', 44 height: 40 45 }, 46}); 47 48const StickyTable = () => { 49 const classes = useStyles(); 50 51 const [items, setItems] = useState([]), 52 [newWord, setNewWord] = useState(''), 53 [newMeaning, setNewMeaning] = useState(''); 54 55 const ref = db.collection("todo"); 56 57 useEffect (() => { 58 const unSub = ref.orderBy('timestamp', 'desc').onSnapshot((snapshot) => { 59 setItems( 60 snapshot.docs.map((doc) => ({id: doc.id, word: doc.data().word, meaning: doc.data().meaning, isCompleted: doc.data().isCompleted})) 61 // snapshot.docs.map((doc) => ({id: doc.id, item: doc.data().item, isCompleted: doc.data().isCompleted})) 62 ); 63 }); 64 return () => unSub(); 65 }, []); 66 67 const handleNewWord = (e) => { 68 setNewWord(e.target.value); 69 // setItems(e.target.value); 70 } 71 const handleNewMeaning = (e) => { 72 setNewMeaning(e.target.value); 73 // setItems(e.target.value); 74 }; 75 76 const onClickAdd = () => { 77 if (newWord === '' || newMeaning === '') { 78 alert('単語と意味の両方を入力してください。'); 79 return; 80 } else if(newWord.match(/[^A-Za-z0-9]+/)) { 81 alert('英語で入力してください。'); 82 return; 83 } else { 84 handleAddWord(); 85 } 86 } 87 88 // const handleAddWord = () => { 89 // db.collection("todo").add({id: uuidv4(), word: newWord, meaning: newMeaning, isCompleted: false, 90 // timestamp: firebase.firestore.FieldValue.serverTimestamp() 91 // }); 92 // setNewWord(''); 93 // setNewMeaning(''); 94 // }; 95 96 const handleAddWord = () => { 97 ref.add({word: newWord, meaning: newMeaning, isCompleted: false, 98 timestamp: firebase.firestore.FieldValue.serverTimestamp() 99 }) 100 .then((doc) => { 101 console.log(`追加に成功しました (${doc.id})`); 102 }) 103 .catch((error) => { 104 console.log(`追加に失敗しました (${error})`); 105 }); 106 setNewWord(''); 107 setNewMeaning(''); 108 }; 109 110 const onClickDelete = () => { 111 ref.doc(id).delete(); 112 // db.collection("todo").get().then(function(snapshot){ 113 // snapshot.forEach(function(doc){ 114 // db.collection("todo").doc(doc.id).delete(); 115 // }) 116 // }) 117 }; 118 119 const speak = (setNewWord) => { 120 let options = new SpeechSynthesisUtterance(setNewWord); 121 options.lang = 'en-US'; 122 speechSynthesis.speak(options); 123 console.log('options') 124 }; 125 126 return ( 127 <> 128 <div className={classes.main}> 129 <form className={classes.paper} autoComplete='off'> 130 <TextField label='単語' value={newWord} onChange={handleNewWord}/> 131 <TextField label='意味' value={newMeaning} onChange={handleNewMeaning}/> 132 <Button disabled={!newWord|!newMeaning} type='button' onClick={onClickAdd}> 133 <AddBox /> 134 </Button> 135 <Button type='button' onClick={onClickDelete}> 136 <DeleteForeverIcon /> 137 </Button> 138 <div> 139 {items.length} items 140 </div> 141 </form> 142 <div className={classes.container}> 143 <List component='ul' className={classes.list} > 144 {items.map((item) => ( 145 <ListItem key={item.id} component='li' className={classes.item}> 146 ・ 147 <Button onClick={onClickDelete}> 148 x 149 </Button> 150 {item.word} 151 <Button type='button' onClick={() => speak(item.word)}> 152 <AudiotrackIcon fontSize='small'/> 153 </Button> 154 </ListItem> 155 ))} 156 </List> 157 <List component='ul' className={classes.list}> 158 {items.map((item) => ( 159 <ListItem key={item.id} component='li' className={classes.item}> 160 {item.meaning} 161 </ListItem> 162 ))} 163 </List> 164 </div> 165 </div> 166 </> 167 ); 168} 169 170export default StickyTable; 171 172 173 174 175 176 177 178 179
あなたの回答
tips
プレビュー