Skip to content

Commit d3726e4

Browse files
committed
fix: extract renderImage outside of render method
1 parent 5d04e35 commit d3726e4

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

packages/scratch-gui/src/components/library-item/library-item.jsx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {FormattedMessage} from 'react-intl';
22
import PropTypes from 'prop-types';
3+
import bindAll from 'lodash.bindall';
34
import React from 'react';
45

56
import Box from '../box/box.jsx';
@@ -15,26 +16,34 @@ import {PLATFORM} from '../../lib/platform.js';
1516

1617
/* eslint-disable react/prefer-stateless-function */
1718
class LibraryItemComponent extends React.PureComponent {
18-
render () {
19-
const renderImage = (className, imageSource) => {
20-
// Scratch Android and Scratch Desktop assume the user is offline and has
21-
// local access to the image assets. In those cases we use the `ScratchImage`
22-
// component which loads the local assets by using a queue. In Scratch Web
23-
// we don't have the assets locally and want to directly download them from
24-
// the assets service.
25-
// TODO: Abstract this logic in the `ScratchImage` component itself.
26-
const url = imageSource.rawUrl ?? imageSource.assetServiceUri;
27-
return this.props.platform === PLATFORM.ANDROID ||
28-
this.props.platform === PLATFORM.DESKTOP ?
29-
<ScratchImage
30-
className={className}
31-
imageSource={imageSource}
32-
/> :
33-
<img
34-
className={className}
35-
src={url}
36-
/>
19+
constructor (props) {
20+
super(props);
21+
bindAll(this, [
22+
'renderImage'
23+
]);
24+
}
25+
renderImage (className, imageSource) {
26+
// Scratch Android and Scratch Desktop assume the user is offline and has
27+
// local access to the image assets. In those cases we use the `ScratchImage`
28+
// component which loads the local assets by using a queue. In Scratch Web
29+
// we don't have the assets locally and want to directly download them from
30+
// the assets service.
31+
// TODO: Abstract this logic in the `ScratchImage` component itself.
32+
const url = imageSource.rawUrl ?? imageSource.assetServiceUri;
33+
34+
if (this.props.platform === PLATFORM.ANDROID ||
35+
this.props.platform === PLATFORM.DESKTOP) {
36+
return (<ScratchImage
37+
className={className}
38+
imageSource={imageSource}
39+
/>);
3740
}
41+
return (<img
42+
className={className}
43+
src={url}
44+
/>);
45+
}
46+
render () {
3847
return this.props.featured ? (
3948
<div
4049
className={classNames(
@@ -59,7 +68,7 @@ class LibraryItemComponent extends React.PureComponent {
5968
</div>
6069
) : null}
6170
{this.props.iconSource ? (
62-
renderImage(styles.featuredImage, this.props.iconSource)
71+
this.renderImage(styles.featuredImage, this.props.iconSource)
6372
) : null}
6473
</div>
6574
{this.props.insetIconURL ? (
@@ -148,7 +157,7 @@ class LibraryItemComponent extends React.PureComponent {
148157
onMouseEnter={this.props.showPlayButton ? this.props.onMouseEnter : null}
149158
onMouseLeave={this.props.showPlayButton ? this.props.onMouseLeave : null}
150159
>
151-
{renderImage(styles.libraryItemImage, this.props.iconSource)}
160+
{this.renderImage(styles.libraryItemImage, this.props.iconSource)}
152161
</Box>
153162
</Box>
154163
<span className={styles.libraryItemName}>{this.props.name}</span>

packages/scratch-gui/src/containers/library-item.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,9 @@ class LibraryItem extends React.PureComponent {
140140
}
141141
}
142142

143-
const mapStateToProps = (state) => {
144-
return {
145-
platform: state.scratchGui.platform.platform,
146-
};
147-
};
143+
const mapStateToProps = state => ({
144+
platform: state.scratchGui.platform.platform
145+
});
148146

149147
LibraryItem.propTypes = {
150148
bluetoothRequired: PropTypes.bool,

0 commit comments

Comments
 (0)