Added check to make sure that FileAttachment is mounted before asynchronously setting state to avoid warning messages.

Этот коммит содержится в:
hmhealey
2015-07-28 12:02:29 -04:00
родитель 411f8cb9ef
Коммит b0c64c73f9

Просмотреть файл

@@ -5,6 +5,7 @@ var utils = require('../utils/utils.jsx');
module.exports = React.createClass({ module.exports = React.createClass({
displayName: "FileAttachment", displayName: "FileAttachment",
canSetState: false,
propTypes: { propTypes: {
filenames: React.PropTypes.arrayOf(React.PropTypes.string).isRequired, filenames: React.PropTypes.arrayOf(React.PropTypes.string).isRequired,
index: React.PropTypes.number.isRequired, index: React.PropTypes.number.isRequired,
@@ -15,6 +16,8 @@ module.exports = React.createClass({
return {fileSize: -1}; return {fileSize: -1};
}, },
componentDidMount: function() { componentDidMount: function() {
this.canSetState = true;
var filename = this.props.filenames[this.props.index]; var filename = this.props.filenames[this.props.index];
var self = this; var self = this;
@@ -50,6 +53,10 @@ module.exports = React.createClass({
} }
} }
}, },
componentWillUnmount: function() {
// keep track of when this component is mounted so that we can asynchronously change state without worrying about whether or not we're mounted
this.canSetState = false;
},
shouldComponentUpdate: function(nextProps, nextState) { shouldComponentUpdate: function(nextProps, nextState) {
// the only time this object should update is when it receives an updated file size which we can usually handle without re-rendering // the only time this object should update is when it receives an updated file size which we can usually handle without re-rendering
if (nextState.fileSize != this.state.fileSize) { if (nextState.fileSize != this.state.fileSize) {
@@ -85,7 +92,9 @@ module.exports = React.createClass({
// asynchronously request the size of the file so that we can display it next to the thumbnail // asynchronously request the size of the file so that we can display it next to the thumbnail
utils.getFileSize(utils.getFileUrl(filename), function(fileSize) { utils.getFileSize(utils.getFileUrl(filename), function(fileSize) {
self.setState({fileSize: fileSize}); if (self.canSetState) {
self.setState({fileSize: fileSize});
}
}); });
} }
@@ -104,7 +113,7 @@ module.exports = React.createClass({
<div className="post-image__name">{fileInfo.name}</div> <div className="post-image__name">{fileInfo.name}</div>
<div> <div>
<span className="post-image__type">{fileInfo.ext.toUpperCase()}</span> <span className="post-image__type">{fileInfo.ext.toUpperCase()}</span>
<span className="post-image__size">{this.fileSizeString}</span> <span className="post-image__size">{fileSizeString}</span>
</div> </div>
</div> </div>
</div> </div>