gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
hello
- User
hello
- Assistant
Hello! How may I assist you today?
- User
Hello! I have this react code: import {useEffect, useRef, useState} from 'react'
function SVGPolygonTest() { const [random, setRandom] = useState(false);
const addPolys = (n) => { setPolys(generatePolygons(polys.length + n)); } const generatePolygons = (n) => { let result = []; if (random) { for (let i = 0; i < n; i++)result.push({x: Math.round(Math.random() * 900), y: Math.round(Math.random() * 900)}) } else { let step = (Math.PI * 2) / n; for (let i = 0; i < n; i++) { result.push({x: 500 + Math.sin(i * step) * 200, y: 500 + Math.cos(i * step) * 200}) } } return result; } const [polys, setPolys] = useState(generatePolygons(10)); const counter = useRef(0); const updatePolys = (polys) => { let result = []; const newCounter = (counter.current + 0.05) % 1000; for (let i = 0; i < polys.length; i++) { let curX = polys[i].x + Math.sin(newCounter) * 30; let curY = polys[i].y + Math.cos(newCounter) * 30; result.push({x: curX, y: curY}); } counter.current = newCounter; let polyTarget = document.getElementById("polyTarget"); polyTarget.setAttribute("points", getPolyData(result)); requestAnimationFrame(() => updatePolys(polys)) }; const getPolyData = (arr) => { let result = ""; arr.forEach((item) => { result += ` ${item.x}, ${item.y}`; }) return result; } useEffect(() => { updatePolys(polys); }, []); return ( <div> <div style={{display: "flex", justifyContent: "flex-end"}}> POLYS: {polys.length} {" "} <button onClick={() => addPolys(50)}>ADD 50</button> <button onClick={() => addPolys(1000)}>ADD 1000</button> <input type="checkbox" onClick={() => setRandom(!random)}/>RANDOM </div> <svg width="1000" height="1000" xmlns="http://www.w3.org/2000/svg"> <polygon id="polyTarget" points="0,0" stroke="black" /> </svg> </div> ) - Assistant
To fix the issue, you need to include the
polysstate as a dependency in theuseEffecthook that calls theupdatePolysfunction. This will make sure that the function is re-run whenever the state changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.