Skip to content

Commit b80d67e

Browse files
shoeman22mrbinky3000
authored andcommitted
Updates for React.StrictMode compliance (#205)
* updated to new context api * fixed warning about setState on unmounted component * added build * moved context provider inside of Tag * re-removed build folder * removed dist folder
1 parent 8db2adf commit b80d67e

File tree

6 files changed

+39
-28
lines changed

6 files changed

+39
-28
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Context from './context';
2+
3+
export default Context.Consumer;

src/CarouselProvider/CarouselProvider.jsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import Context from './context';
34
import Store from '../Store/Store';
45
import {
56
CarouselPropTypes, slideSize, slideTraySize, cn,
@@ -31,10 +32,6 @@ const CarouselProvider = class CarouselProvider extends React.Component {
3132
infinite: PropTypes.bool,
3233
};
3334

34-
static childContextTypes = {
35-
carouselStore: PropTypes.object,
36-
};
37-
3835
static defaultProps = {
3936
className: null,
4037
currentSlide: 0,
@@ -56,8 +53,8 @@ const CarouselProvider = class CarouselProvider extends React.Component {
5653
infinite: false,
5754
};
5855

59-
constructor(props, context) {
60-
super(props, context);
56+
constructor(props) {
57+
super(props);
6158
const options = {
6259
currentSlide: props.currentSlide,
6360
disableAnimation: props.disableAnimation,
@@ -88,10 +85,6 @@ const CarouselProvider = class CarouselProvider extends React.Component {
8885
this.carouselStore = new Store(options);
8986
}
9087

91-
getChildContext() {
92-
return { carouselStore: this.carouselStore };
93-
}
94-
9588
componentDidUpdate(prevProps) {
9689
const newStoreState = {};
9790

@@ -189,7 +182,9 @@ const CarouselProvider = class CarouselProvider extends React.Component {
189182

190183
return (
191184
<Tag className={newClassName} {...filteredProps}>
192-
{this.props.children}
185+
<Context.Provider value={this.carouselStore}>
186+
{this.props.children}
187+
</Context.Provider>
193188
</Tag>
194189
);
195190
}

src/CarouselProvider/context.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
3+
const Context = React.createContext();
4+
export default Context;

src/CarouselProvider/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
import CarouselProvider from './CarouselProvider';
2+
import CarouselContext from './context';
3+
import CarouselConsumer from './CarouselConsumer';
4+
5+
export {
6+
CarouselProvider,
7+
CarouselContext,
8+
CarouselConsumer,
9+
};
210

311
export default CarouselProvider;

src/Store/WithStore.jsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import React from 'react';
22
import equal from 'equals';
33
import deepMerge from 'deepmerge';
4-
import propTypes from 'prop-types';
54
import { CarouselPropTypes } from '../helpers';
5+
import { CarouselContext } from '../CarouselProvider';
66

77
export default function WithStore(
88
WrappedComponent,
99
/* istanbul ignore next */ mapStateToProps = () => ({}),
1010
) {
1111
class Wrapper extends React.Component {
12+
static contextType = CarouselContext
13+
1214
static propTypes = {
1315
children: CarouselPropTypes.children,
1416
};
@@ -17,15 +19,14 @@ export default function WithStore(
1719
children: null,
1820
};
1921

20-
static contextTypes = {
21-
carouselStore: propTypes.object,
22-
};
23-
2422
constructor(props, context) {
2523
super(props, context);
26-
this.state = mapStateToProps({ ...context.carouselStore.state });
24+
this.state = mapStateToProps({ ...context.state });
2725
this.updateStateProps = this.updateStateProps.bind(this);
28-
this.context.carouselStore.subscribe(this.updateStateProps);
26+
}
27+
28+
componentDidMount() {
29+
this.context.subscribe(this.updateStateProps);
2930
}
3031

3132
shouldComponentUpdate(nextProps, nextState) {
@@ -34,11 +35,11 @@ export default function WithStore(
3435
}
3536

3637
componentWillUnmount() {
37-
this.context.carouselStore.unsubscribe(this.updateStateProps);
38+
this.context.unsubscribe(this.updateStateProps);
3839
}
3940

4041
updateStateProps() {
41-
this.setState(mapStateToProps({ ...this.context.carouselStore.state }));
42+
this.setState(mapStateToProps({ ...this.context.state }));
4243
}
4344

4445
render() {
@@ -51,13 +52,13 @@ export default function WithStore(
5152
}} // allows access to refs in wrapped components.
5253
{...props}
5354
carouselStore={{
54-
getStoreState: this.context.carouselStore.getStoreState,
55-
masterSpinnerError: this.context.carouselStore.masterSpinnerError,
56-
masterSpinnerSuccess: this.context.carouselStore.masterSpinnerSuccess,
57-
setStoreState: this.context.carouselStore.setStoreState,
58-
subscribeMasterSpinner: this.context.carouselStore.subscribeMasterSpinner,
59-
unsubscribeAllMasterSpinner: this.context.carouselStore.unsubscribeAllMasterSpinner,
60-
unsubscribeMasterSpinner: this.context.carouselStore.unsubscribeMasterSpinner,
55+
getStoreState: this.context.getStoreState,
56+
masterSpinnerError: this.context.masterSpinnerError,
57+
masterSpinnerSuccess: this.context.masterSpinnerSuccess,
58+
setStoreState: this.context.setStoreState,
59+
subscribeMasterSpinner: this.context.subscribeMasterSpinner,
60+
unsubscribeAllMasterSpinner: this.context.unsubscribeAllMasterSpinner,
61+
unsubscribeMasterSpinner: this.context.unsubscribeMasterSpinner,
6162
}}
6263
>
6364
{this.props.children}

src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import React from 'react';
33
import ReactDOM from 'react-dom';
44
import DevelopmentApp from './App/App';
55

6-
ReactDOM.render(<DevelopmentApp />, document.getElementById('app'));
6+
ReactDOM.render(<React.StrictMode><DevelopmentApp /></React.StrictMode>, document.getElementById('app'));

0 commit comments

Comments
 (0)