前提・実現したいこと
・配列の引数に変数を使って値を代入したいです。
・最終的には、3x3の9マスのランダムな位置に、ランダムな数字を、1秒間隔で表示させ続けたいです。
・ReactチュートリアルをTypeScriptにしたものを参考にコーディングしています。
https://www.membersedge.co.jp/blog/refactor-react-official-tutorial/
発生している問題・エラーメッセージ
React
1Type '{ count: number; }' cannot be used as an index type. TS2538 2 3 40 | const interval = setInterval(() => { 4 41 | setCount(c => c + 1); 5 > 42 | squares[items[{count}].position] = items[{count}].value; 6 | ^ 7 43 | }, 1000); 8 44 | return () => clearInterval(interval); 9 45 | }, []);
該当のソースコード
React
1import React, { useState, useEffect } from 'react'; 2import './Borad.css'; 3import Square from './Square'; 4 5const Borad: React.FC = () => { 6 const [count, setCount] = useState(0); 7 8 var squares = [0,0,0,0,0,0,0,0,0]; 9 10 const items = [ 11 {position: 0, value: 2}, 12 {position: 1, value: 2}, 13 {position: 2, value: 2}, 14 {position: 3, value: 2}, 15 {position: 4, value: 2}, 16 {position: 5, value: 2}, 17 {position: 6, value: 2}, 18 {position: 7, value: 2}, 19 {position: 8, value: 2}, 20 ] 21 22 squares[items[2].position] = items[2].value; 23 24 useEffect(() => { 25 const interval = setInterval(() => { 26 setCount(c => c + 1); 27 squares[items[{count}].position] = items[{count}].value; 28 }, 1000); 29 return () => clearInterval(interval); 30 }, []); 31 32 return( 33 <> 34 {count}<br/><br/> 35 <div> 36 {[...Array(3)].map((_, i) => { 37 return ( 38 <div className="board-row" key={i}> 39 {[...Array(3)].map((_, j) => { 40 const index = 3 * i + j; 41 return ( 42 <Square 43 value={squares[index]} 44 key={j} 45 /> 46 ); 47 })} 48 </div> 49 ); 50 })} 51 </div> 52 </> 53 ) 54}; 55 56export default Borad;
試したこと
・変数ではなく値を直接入れた場合は、問題なく動作します。
・具体的には、22行目の以下の箇所です。
squares[items[2].position] = items[2].value;
補足情報(FW/ツールのバージョンなど)
package.json
1{ 2 "name": "my-app", 3 "version": "0.1.0", 4 "private": true, 5 "dependencies": { 6 "@testing-library/jest-dom": "^5.11.4", 7 "@testing-library/react": "^11.1.0", 8 "@testing-library/user-event": "^12.1.10", 9 "@types/jest": "^26.0.15", 10 "@types/node": "^12.0.0", 11 "@types/react": "^16.9.53", 12 "@types/react-dom": "^16.9.8", 13 "react": "^17.0.1", 14 "react-dom": "^17.0.1", 15 "react-scripts": "4.0.1", 16 "typescript": "^4.1.2", 17 "web-vitals": "^0.2.4" 18 }, 19 "scripts": { 20 "start": "react-scripts start", 21 "build": "react-scripts build", 22 "test": "react-scripts test", 23 "eject": "react-scripts eject" 24 }, 25 "eslintConfig": { 26 "extends": [ 27 "react-app", 28 "react-app/jest" 29 ] 30 }, 31 "browserslist": { 32 "production": [ 33 ">0.2%", 34 "not dead", 35 "not op_mini all" 36 ], 37 "development": [ 38 "last 1 chrome version", 39 "last 1 firefox version", 40 "last 1 safari version" 41 ] 42 } 43}
追記:{count}の括弧{}を外した場合
◆修正後のコード
useEffect(() => { const interval = setInterval(() => { setCount(c => c + 1); squares[items[count].position] = items[count].value; }, 1000); return () => clearInterval(interval); }, []);
◆エラー内容
・コンソール
Compiled with warnings. src/components/Borad.tsx Line 30:6: React Hook useEffect has missing dependencies: 'count', 'items', and 'squares'. Either include them or remove the dependency array react-hooks/exhaustive-deps Search for the keywords to learn more about each warning. To ignore, add // eslint-disable-next-line to the line before.
TypeError: Cannot read property 'position' of undefined (anonymous function) src/components/Borad.tsx:27 24 | useEffect(() => { 25 | const interval = setInterval(() => { 26 | setCount(c => c + 1); > 27 | squares[items[count].position] = items[count].value; | ^ 28 | }, 1000); 29 | return () => clearInterval(interval); 30 | }, []);
回答1件
あなたの回答
tips
プレビュー