Skip to content

Updates for React.StrictMode compliance #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/CarouselProvider/CarouselConsumer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Context from './context';

export default Context.Consumer;
17 changes: 6 additions & 11 deletions src/CarouselProvider/CarouselProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import Context from './context';
import Store from '../Store/Store';
import {
CarouselPropTypes, slideSize, slideTraySize, cn,
Expand Down Expand Up @@ -31,10 +32,6 @@ const CarouselProvider = class CarouselProvider extends React.Component {
infinite: PropTypes.bool,
};

static childContextTypes = {
carouselStore: PropTypes.object,
};

static defaultProps = {
className: null,
currentSlide: 0,
Expand All @@ -56,8 +53,8 @@ const CarouselProvider = class CarouselProvider extends React.Component {
infinite: false,
};

constructor(props, context) {
super(props, context);
constructor(props) {
super(props);
const options = {
currentSlide: props.currentSlide,
disableAnimation: props.disableAnimation,
Expand Down Expand Up @@ -88,10 +85,6 @@ const CarouselProvider = class CarouselProvider extends React.Component {
this.carouselStore = new Store(options);
}

getChildContext() {
return { carouselStore: this.carouselStore };
}

componentDidUpdate(prevProps) {
const newStoreState = {};

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

return (
<Tag className={newClassName} {...filteredProps}>
{this.props.children}
<Context.Provider value={this.carouselStore}>
{this.props.children}
</Context.Provider>
</Tag>
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/CarouselProvider/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';

const Context = React.createContext();
export default Context;
8 changes: 8 additions & 0 deletions src/CarouselProvider/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import CarouselProvider from './CarouselProvider';
import CarouselContext from './context';
import CarouselConsumer from './CarouselConsumer';

export {
CarouselProvider,
CarouselContext,
CarouselConsumer,
};

export default CarouselProvider;
33 changes: 17 additions & 16 deletions src/Store/WithStore.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React from 'react';
import equal from 'equals';
import deepMerge from 'deepmerge';
import propTypes from 'prop-types';
import { CarouselPropTypes } from '../helpers';
import { CarouselContext } from '../CarouselProvider';

export default function WithStore(
WrappedComponent,
/* istanbul ignore next */ mapStateToProps = () => ({}),
) {
class Wrapper extends React.Component {
static contextType = CarouselContext

static propTypes = {
children: CarouselPropTypes.children,
};
Expand All @@ -17,15 +19,14 @@ export default function WithStore(
children: null,
};

static contextTypes = {
carouselStore: propTypes.object,
};

constructor(props, context) {
super(props, context);
this.state = mapStateToProps({ ...context.carouselStore.state });
this.state = mapStateToProps({ ...context.state });
this.updateStateProps = this.updateStateProps.bind(this);
this.context.carouselStore.subscribe(this.updateStateProps);
}

componentDidMount() {
this.context.subscribe(this.updateStateProps);
}

shouldComponentUpdate(nextProps, nextState) {
Expand All @@ -34,11 +35,11 @@ export default function WithStore(
}

componentWillUnmount() {
this.context.carouselStore.unsubscribe(this.updateStateProps);
this.context.unsubscribe(this.updateStateProps);
}

updateStateProps() {
this.setState(mapStateToProps({ ...this.context.carouselStore.state }));
this.setState(mapStateToProps({ ...this.context.state }));
}

render() {
Expand All @@ -51,13 +52,13 @@ export default function WithStore(
}} // allows access to refs in wrapped components.
{...props}
carouselStore={{
getStoreState: this.context.carouselStore.getStoreState,
masterSpinnerError: this.context.carouselStore.masterSpinnerError,
masterSpinnerSuccess: this.context.carouselStore.masterSpinnerSuccess,
setStoreState: this.context.carouselStore.setStoreState,
subscribeMasterSpinner: this.context.carouselStore.subscribeMasterSpinner,
unsubscribeAllMasterSpinner: this.context.carouselStore.unsubscribeAllMasterSpinner,
unsubscribeMasterSpinner: this.context.carouselStore.unsubscribeMasterSpinner,
getStoreState: this.context.getStoreState,
masterSpinnerError: this.context.masterSpinnerError,
masterSpinnerSuccess: this.context.masterSpinnerSuccess,
setStoreState: this.context.setStoreState,
subscribeMasterSpinner: this.context.subscribeMasterSpinner,
unsubscribeAllMasterSpinner: this.context.unsubscribeAllMasterSpinner,
unsubscribeMasterSpinner: this.context.unsubscribeMasterSpinner,
}}
>
{this.props.children}
Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import React from 'react';
import ReactDOM from 'react-dom';
import DevelopmentApp from './App/App';

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