생활
자바스크립트 react관련질문좀합니다
버튼을 클릭하면 사각형을 특정 공간만큼 움직이려고 하거든요.
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
marginElem: document.getElementById("sq")
}
this.onclick = this.onclick.bind(this);
}
onclick(e) {
this.setState({ marginElem: this.state.marginElem.style.marginLeft + "1.5rem"});
}
render() {
return (
<div style={{marginTop:"3rem", marginLeft: "3rem"}}>
<div id="sq" className="square"></div>
<div>
<input onClick={this.onclick} className="button" type="button" value="click" />
</div>
</div>
);
}
}
export default App;
버튼을 클릭하면 null 이 발생하여 에러가 뜨거든요. 상태를 업데이트 시켜주면 되는거 같은데 어느 부분이 잘못된 건가요?
2개의 답변이 있어요!