1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import { useState } from 'react';
let initialShapes = [ { id: 0, type: 'circle', x: 50, y: 100 }, { id: 1, type: 'square', x: 150, y: 100 }, { id: 2, type: 'circle', x: 250, y: 100 }, ];
export default function ShapeEditor() { const [shapes, setShapes] = useState( initialShapes );
function handleClick() { const nextShapes = shapes.map(shape => { if (shape.type === 'square') { return shape; } else { return { ...shape, y: shape.y + 50, }; } }); setShapes(nextShapes); }
return ( <> <button onClick={handleClick}> 所有圆形向下移动! </button> {shapes.map(shape => ( <div key={shape.id} style={{ background: 'purple', position: 'absolute', left: shape.x, top: shape.y, borderRadius: shape.type === 'circle' ? '50%' : '', width: 20, height: 20, }} /> ))} </> ); }
|