Skip to content

Commit 438cdfc

Browse files
authored
refactor: state constructor simplifications (react-bootstrap#3993)
1 parent e039472 commit 438cdfc

File tree

10 files changed

+35
-52
lines changed

10 files changed

+35
-52
lines changed

src/AbstractNav.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ class AbstractNav extends React.Component {
2727
activeKey: PropTypes.any,
2828
};
2929

30-
constructor(...args) {
31-
super(...args);
32-
33-
this.state = { navContext: null };
34-
}
30+
state = {
31+
navContext: null,
32+
};
3533

3634
static getDerivedStateFromProps({
3735
activeKey,

src/Carousel.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,14 @@ const defaultProps = {
120120
};
121121

122122
class Carousel extends React.Component {
123-
constructor(props, context) {
124-
super(props, context);
123+
state = {
124+
prevClasses: '',
125+
currentClasses: 'active',
126+
};
125127

126-
this.state = {
127-
prevClasses: '',
128-
currentClasses: 'active',
129-
};
130-
this.isUnmounted = false;
128+
isUnmounted = false;
131129

132-
this.carousel = React.createRef();
133-
}
130+
carousel = React.createRef();
134131

135132
componentDidMount() {
136133
this.cycle();

src/Modal.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,11 @@ function BackdropTransition(props) {
183183
/* eslint-enable no-use-before-define */
184184

185185
class Modal extends React.Component {
186-
constructor(props, context) {
187-
super(props, context);
186+
state = { style: {} };
188187

189-
this.state = { style: {} };
190-
this.modalContext = {
191-
onHide: () => this.props.onHide(),
192-
};
193-
}
188+
modalContext = {
189+
onHide: () => this.props.onHide(),
190+
};
194191

195192
componentWillUnmount() {
196193
// Clean up the listener if we need to.

src/Navbar.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,11 @@ const defaultProps = {
123123
};
124124

125125
class Navbar extends React.Component {
126-
constructor(...args) {
127-
super(...args);
128-
129-
this.state = {
130-
navbarContext: {
131-
onToggle: this.handleToggle,
132-
},
133-
};
134-
}
126+
state = {
127+
navbarContext: {
128+
onToggle: () => this.handleToggle(),
129+
},
130+
};
135131

136132
static getDerivedStateFromProps({ bsPrefix, expanded }, prevState) {
137133
return {

src/TabContainer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class TabContainer extends React.Component {
8181
activeKey: PropTypes.any,
8282
};
8383

84-
constructor(...args) {
85-
super(...args);
84+
constructor(props) {
85+
super(props);
8686

8787
this.state = {
8888
tabContext: {

src/ThemeProvider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class ThemeProvider extends React.Component {
1010
prefixes: PropTypes.object.isRequired,
1111
};
1212

13-
constructor(...args) {
14-
super(...args);
13+
constructor(props) {
14+
super(props);
1515
this.prefixes = new Map();
1616
Object.keys(this.props.prefixes).forEach(key => {
1717
this.prefixes.set(key, this.props.prefixes[key]);

src/Toast.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ const Toast = ({
7979
};
8080
}
8181
return () => null;
82-
}, [autohide, show]);
82+
// also clear the Timeout of the delay has changed
83+
// eslint-disable-next-line react-hooks/exhaustive-deps
84+
}, [autohide, onClose, show]);
8385
const useAnimation = Transition && animation;
8486
const toast = (
8587
<div

test/ModalSpec.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,9 @@ describe('<Modal>', () => {
182182

183183
it('should remove resize listener when unmounted', () => {
184184
class Component extends React.Component {
185-
constructor(props, context) {
186-
super(props, context);
187-
188-
this.state = {
189-
show: true,
190-
};
191-
}
185+
state = {
186+
show: true,
187+
};
192188

193189
render() {
194190
if (!this.state.show) {

test/OverlayTriggerSpec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,13 @@ describe('<OverlayTrigger>', () => {
283283
describe('replaced overlay', () => {
284284
it('Should still be shown', () => {
285285
class ReplacedOverlay extends React.Component {
286-
constructor(props) {
287-
super(props);
286+
state = {
287+
replaced: false,
288+
};
288289

289-
this.handleClick = this.handleClick.bind(this);
290-
this.state = { replaced: false };
291-
}
292-
293-
handleClick() {
290+
handleClick = () => {
294291
this.setState({ replaced: true });
295-
}
292+
};
296293

297294
render() {
298295
if (this.state.replaced) {

www/src/components/Sonnet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22

33
class Sonnet extends React.Component {
4-
constructor(...args) {
5-
super(...args);
4+
constructor(props) {
5+
super(props);
66
import('shakespeare-data').then(s =>
77
this.setState({ sonnet: s.sonnets.random() }),
88
);

0 commit comments

Comments
 (0)