我正在尝试更新用 React.createElement() 创建的对象。我要更新的属性是particleCopy.props.style.top
. 下面是我的代码。
import React, { useState } from 'react';
import './Particles.scss';
export default function Particles() {
const particleStyle = { color: 'blue', position: 'absolute', top: '0px' };
const [particle, setParticle] = useState(
React.createElement('div', { style: particleStyle }, '*')
);
const moveParticleDown = (particle, top) => {
const particleCopy = Object.assign({}, particle);
particleCopy.props.style.top = top + 'px';
setParticle(particleCopy);
};
return (
<div className="particles_container">
<div className="particles">{particle}</div>
<div className="controls">
<button onClick={() => moveParticleDown(particle, 10)}>down</button>
</div>
</div>
);
}
我收到以下错误
Uncaught TypeError: Cannot assign to read only property 'top' of object '#<Object>'