componentDidMount에서 this.state.board에 액세스 할 수없는 이유는 무엇입니까? 일단 컴포넌트가 렌더링되면 componentDidMount는 즉시 한 번만 실행됩니다.componentDidMount의 상태 데이터에 액세스
Google 애널리틱스 전자 상거래 추적 프로그램을 설정하려고하는데이를 설정하는 가장 좋은 장소는 GA 추적기가 한 번만 호출되도록하기 때문에 componentDidMount 내에 있어야한다고 생각했습니다. 그러나 GA로 다시 보내려면 상태 데이터에 액세스 할 수 없습니다. 어떤 아이디어?
//function which establishes state
function getEditorState() {
var board = Editor.getCsb();
var similarBoard = Editor.getSimilarLsbs();
return {
board: board,
similarBoard: similarBoard,
editing: Editor.isEditing(),
hoverColor: Editor.getHoverColor(),
variant: Editor.variant(),
lidAndLsb: Editor.getLidAndLsb()
};
}
//component
var CsbEditorApp = React.createClass({
getInitialState: function() {
return getEditorState();
},
componentDidMount: function() {
console.log(this.state.board); // <---- this returns an empty object.
Editor.addChangeListener(this._onChange);
SbAction.loadCsb(this.props.params.cid);
},
render: function() {
console.log(this.state.board); // <---- this returns the the board object with data.
return (
<div className={cm('lightbox sb-bg overlay-border')} ref="app">
<Header board={this.state.board} label={this.state.label} socialLinkStatus={this.state.socialLinkStatus} buyingAll={this.state.buyingAll} />
<div className="viewer-content">
<div id="csb-content">
<MetaText className="meta-author" metaKey="author" board={this.state.board} />
<BoardMeta board={this.state.board}/>
<CsbPanel board={this.state.board hoverColor={this.state.hoverColor} showPanel={showPanel} />
<RouteHandler/>
</div>
</div>
</div>
);
},
_onChange: function() {
this.setState(getEditorState());
$("#cc_hover").hide();
}
});
몇 가지 코드를 제공해 주시겠습니까? – LuisPinto
@ LuisPinto 내 코드 –
을 추가했는데 구성 요소 안에 getEditorState()를 넣을 수 있습니까? – LuisPinto