Skip to content

Commit 1fea984

Browse files
trembyaaronshaf
authored andcommitted
Fix lint errors (#153)
* Lint removal: whitespace * Lint removal: semicolon * Lint removal: silence false-positive Quoting from https://reactjs.org/docs/react-component.html#componentdidupdate > componentDidUpdate(prevProps) { > // Typical usage (don't forget to compare props): > if (this.props.userID !== prevProps.userID) { > this.fetchData(this.props.userID); > } > } > > You may call **`setState()`** immediately in `componentDidUpdate()` > but note that **it must be wrapped in a condition** like in the > example above, or you’ll cause an infinite loop. It would also cause > an extra re-rendering which, while not visible to the user, can > affect the component performance. If you’re trying to “mirror” some > state to a prop coming from above, consider using the prop directly > instead. Read more about why copying props into state causes bugs. This particular use case is OK by those rules, so we disable the linting rule for that particular line and leave a comment explaining why.
1 parent 91b029c commit 1fea984

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/component/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ export default class Toggle extends PureComponent {
2121
}
2222
}
2323

24-
componentDidUpdate (prevProps) {
25-
if (prevProps.checked !== this.props.checked) {
26-
this.setState({ checked: !!this.props.checked });
27-
}
28-
}
24+
componentDidUpdate (prevProps) {
25+
if (prevProps.checked !== this.props.checked) {
26+
// Disable linting rule here since this usage of setState inside
27+
// componentDidUpdate is OK; see
28+
// https://reactjs.org/docs/react-component.html#componentdidupdate
29+
// eslint-disable-next-line react/no-did-update-set-state
30+
this.setState({ checked: !!this.props.checked })
31+
}
32+
}
2933

3034
handleClick (event) {
3135
if (this.props.disabled) {

src/docs/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class App extends Component {
4040
)
4141

4242
this.state = {
43-
aubergineIsReady: true,
44-
checkedOverride: false,
43+
aubergineIsReady: true,
44+
checkedOverride: false,
4545
cheeseIsReady: false,
4646
baconIsReady: false,
4747
biscuitIsReady: false,
@@ -98,8 +98,8 @@ class App extends Component {
9898

9999
<div className='example'>
100100
<label>
101-
<Toggle
102-
checked={this.state.checkedOverride}
101+
<Toggle
102+
checked={this.state.checkedOverride}
103103
defaultChecked={this.state.baconIsReady}
104104
onChange={this.handleBaconChange}
105105
/>

0 commit comments

Comments
 (0)