Skip to content

Commit

Permalink
Resolves eslint warnings
Browse files Browse the repository at this point in the history
ESLint was triggering warnings that had to be resolved (annoyingly in a single commit!).

Firstly, regarding usage of setState in componentDidMount. This is often true. However React docs themselves say this is valid when doing DOM interaction such as measuring widths…etc.

> …when you need to measure a DOM node before rendering something that depends on its size or position

See https://reactjs.org/docs/react-component.html#componentdidmount

Secondly, string refs were being used. Modified to use createRef() pattern.
  • Loading branch information
getdave committed Jan 25, 2019
1 parent 867a2fe commit b081382
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions client/my-sites/media/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ class Media extends Component {
source: '',
};

constructor( props ) {
super( props );
this.containerRef = React.createRef();
}

componentDidMount() {
// This is deemed valid, see:
// "when you need to measure a DOM node before rendering
// something that depends on its size or position."
// https://reactjs.org/docs/react-component.html#componentdidmount

// eslint-disable-next-line react/no-did-mount-set-state
this.setState( {
containerWidth: this.refs.container.clientWidth,
containerWidth: this.containerRef.clientWidth,
} );
}

Expand Down Expand Up @@ -350,7 +361,7 @@ class Media extends Component {
render() {
const { selectedSite: site, mediaId, previousRoute, translate } = this.props;
return (
<div ref="container" className="main main-column media" role="main">
<div ref={ this.containerRef } className="main main-column media" role="main">
{ mediaId && site && site.ID && <QueryMedia siteId={ site.ID } mediaId={ mediaId } /> }
<PageViewTracker path={ this.getAnalyticsPath() } title="Media" />
<SidebarNavigation />
Expand Down

0 comments on commit b081382

Please sign in to comment.