Skip to content

Commit 7682ec6

Browse files
committed
Extracted the logic to update the <ColorSelector> React 'state' with new value for a color component into separate nested function 'updateStateForColor(...)', to DRY out the code.
1 parent ce5f5ab commit 7682ec6

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

webpack_in/entry.jsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,19 @@ class ColorSelector extends React.Component {
303303
}
304304

305305
render() {
306+
const updateStateForColor = (component, value) => {
307+
const color = {
308+
...this.state.color
309+
};
310+
311+
color[component] = value;
312+
313+
this.setState({
314+
...this.state,
315+
color
316+
});
317+
};
318+
306319
const { color } = this.state;
307320

308321
const colorOpposite = {
@@ -323,35 +336,17 @@ class ColorSelector extends React.Component {
323336
<div style={{ marginTop: '-1em'}}>
324337
<ColorComponentEntry label="R" value={ this.state.color.r } onChangeValue={
325338
(value) => {
326-
this.setState({
327-
...this.state,
328-
color: {
329-
...this.state.color,
330-
r: value
331-
}
332-
});
339+
updateStateForColor('r', value);
333340
}
334341
} />
335342
<ColorComponentEntry label="G" value={ this.state.color.g } onChangeValue={
336343
(value) => {
337-
this.setState({
338-
...this.state,
339-
color: {
340-
...this.state.color,
341-
g: value
342-
}
343-
});
344+
updateStateForColor('g', value);
344345
}
345346
} />
346347
<ColorComponentEntry label="B" value={ this.state.color.b } onChangeValue={
347348
(value) => {
348-
this.setState({
349-
...this.state,
350-
color: {
351-
...this.state.color,
352-
b: value
353-
}
354-
});
349+
updateStateForColor('b', value);
355350
}
356351
} />
357352
</div>

0 commit comments

Comments
 (0)