저는 React에 매우 익숙합니다. 요소의 높이를 가져와야하므로 componentDidMount
메서드에서 가져 오려고합니다. 이 메소드는 컴포넌트를 렌더링 한 후에 호출 된 것으로 이해합니다.이 컴포넌트는 마지막에 실제 DOM을 작성하는 것으로 가정합니다. 그러나 최종 DOM을 사용하려면 먼저 componentDidMount
이 호출되어야합니다. 어째서?실제 DOM을 사용하기 전에 componentDidMount가 호출됩니다.
componentDidMount() {
const el = window.document.getElementById('comments'); // el is null
console.log(el);
}
resize() {
const el = window.document.getElementById('comments'); // el is null
console.log(el);
}
render() {
const { name } = this.props;
const Comments = this.props.comments.filter(comment => comment.body !== null && comment.body !== '').map(comment => <Comment key={comment.id} comment={comment} />);
return (
<div ref={this.resize}>
<div>
<div id="comments">
{ Comments }
</div>
</div>
</div>
);
}
코드 예제를 추가하십시오. –
@RuiCosta 완료 및 감사 – R01010010