Will componentDidUpdate fire earlier in this case?
I know it fires after DOM is updated. But I have met a problem.
I'm working on a piece of code like following.
Here is the component.
function getState() {
return {
layers: Store.getLayers()
};
}
var Component = React.createClass({
getInitialState: function() {
return getState();
},
componentDidMount: function() {
Store.addChangeListener(this._onChange);
this.updateLayerNode();
},
componentDidUpdate: function() {
this.updateLayerNode();
},
componentWillUnmount: function() {
Store.removeChangeListener(this._onChange);
},
render: function() {
return (
<div>
{this.state.layers.map(function(layer) {
return (
<canvas key={layer.id} ref={layer.id}></canvas>
);
})}
</div>
);
},
updateLayerNode: function() {
var self = this;
this.state.layers.forEach(function(layer) {
layer.node = self.refs[layer.id].getDOMNode();
layer.context = layer.node.getContext('2d');
});
},
_onChange: function() {
this.setState(getState());
}
}
Here is the store.
var CHANGE_EVENT = 'change';
var _layers = [];
function _createLayer(id) {
var layer = {
id: id,
node: null,
context: null
};
_layers.push(layer);
}
var Store = assign({}, EventEmitter.prototype, {
getLayers: function() {
return _layers;
},
getLayer: function(id) {
for (var i = 0; i < _layers.length; i++) {
if (_layers[i].id === id) {
return _layers[i];
}
}
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
});
A layer means a <canvas> node. I need to create a layer somewhere else, e.g. in a click handler, and use the CanvasRenderingContext2D to draw something.
Actions.createLayer(id);
var layer = Store.getLayer(id);
layer.context.fillRect(0, 0, 100, 50);
~~I have wrote a demo, it works well.~~ But when I work on my project, getCanvasContext will be invoked before componentDidUpdate. I'm very confused about the execution order.
Update: At first, I just create layer and draw after the component is rendered, it works.
React.render(
(
<div>
<Component />
</div>
),
document.getElementById('demo')
);
Actions.createLayer(id);
var layer = Store.getLayer(id);
layer.context.fillRect(0, 0, 100, 50);
But after I move that into a click handler, it doesn't work anymore.
function onClick() {
Actions.createLayer(id);
var layer = Store.getLayer(id);
layer.context.fillRect(0, 0, 100, 50);
}
React.render(
(
<div>
<Component />
<button onClick={onClick}>Draw</button>
</div>
),
document.getElementById('demo')
);
Are there any ideas to solve my problem?
Can you show more code? I'm not sure I follow completely.
But when I work on my project, getCanvasContext will be invoked before componentDidUpdate
Where do you put it? It's hard to understand because there's no method definition in your example.
Also, I'd be wary of saving anything DOMish in stores, they should hold the data.
@gaearon Thank you. I have updated the code, hope you can understand it clearly now.
Also, I'd be wary of saving anything DOMish in stores, they should hold the data.
I agree. But I haven't thought of a better way to do that.
@byelims could you explain the overall goal? e.g. what is the component supposed to do. There might be a way to do it without DOMish things in the store :)
Closing this out as this doesn't sound like a React issue. If you can post a full code sample then maybe someone can help you debug your problem, but Stack Overflow is probably a better place for code-level debugging questions.