Skip to content

Commit d6936e3

Browse files
committed
Update react-redux to 6.0 + Use context for custom store
The `storeKey` method for supporting multiple stores was dropped in react-redux v6.
1 parent de10661 commit d6936e3

File tree

8 files changed

+34
-39
lines changed

8 files changed

+34
-39
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"name": "react-lifecycle-visualizer-demo",
3-
"version": "0.1",
3+
"version": "1.0",
44
"description": "Dummy configuration for StackBlitz deployment. Don't use this to build the demo; use the top-level package.json instead",
55
"main": "src/index.jsx",
66
"homepage": "https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo",
77
"scripts": {
88
"start": "echo \"Error: This dummy package.json is only for StackBlitz deployment. Use top-level package.json instead.\" && exit 1"
99
},
1010
"devDependencies": {
11-
"react-scripts": "^1.1.5"
11+
"react-scripts": "^2.1.3"
1212
},
1313
"dependencies": {
1414
"prop-types": "^15.6.2",
15-
"react": "16.5.1",
16-
"react-dom": "16.5.1",
17-
"react-hot-loader": "4.3.8",
15+
"react": "16.7.0",
16+
"react-dom": "16.7.0",
17+
"react-hot-loader": "4.6.3",
1818
"react-lifecycle-visualizer": "^2.1.1"
1919
}
2020
}

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"dependencies": {
8787
"hoist-non-react-statics": "^3.3.0",
8888
"prop-types": "^15.6.2",
89-
"react-redux": "^5.1.1",
89+
"react-redux": "^6.0.0",
9090
"redux": "^4.0.1",
9191
"redux-thunk": "^2.3.0"
9292
},

src/components/LifecyclePanel.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { connect } from 'react-redux';
33

44
import * as constants from '../constants';
5+
import { LifecycleVisualizerContext } from '../redux/VisualizerProvider';
56

67
const LifecyclePanel = (props) => {
78
const {componentName, isLegacy, instanceId, highlightedMethod, implementedMethods} = props;
@@ -54,4 +55,4 @@ const mapStateToProps = ({logEntries, highlightedIndex}) => ({
5455
: null
5556
});
5657

57-
export default connect(mapStateToProps, null, null, {storeKey: constants.reduxStoreKey})(LifecyclePanel);
58+
export default connect(mapStateToProps, null, null, {context: LifecycleVisualizerContext})(LifecyclePanel);

src/components/Log.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { connect } from 'react-redux';
44

55
import * as constants from '../constants';
66
import * as ActionCreators from '../redux/actionCreators';
7+
import { LifecycleVisualizerContext } from '../redux/VisualizerProvider';
78
import LogEntries from './LogEntries';
89
import SimpleButton from './SimpleButton';
910

@@ -77,4 +78,4 @@ class Log extends Component {
7778
const mapStateToProps = ({logEntries, highlightedIndex, replayTimerId, replayTimerDelay}) =>
7879
({logEntries, highlightedIndex, replayTimerId, replayTimerDelay});
7980

80-
export default connect(mapStateToProps, ActionCreators, null, {storeKey: constants.reduxStoreKey})(Log);
81+
export default connect(mapStateToProps, ActionCreators, null, {context: LifecycleVisualizerContext})(Log);

src/constants.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const lifecycleMethods = [
4242
export const lifecycleMethodNames =
4343
lifecycleMethods.filter((mthd) => !mthd.isLegacy).map(({name}) => name);
4444

45-
// We don't show UNSAFE_ methods in the panel, but just use the shorter old names.
45+
// We don't show 'UNSAFE_..' in the panel, but just use the shorter old names.
4646
export const lifecycleMethodNamesLegacyNoUnsafe =
4747
lifecycleMethods.filter(
4848
(mthd) => !mthd.isNew && !mthd.name.startsWith('UNSAFE_')
@@ -54,8 +54,6 @@ export const lifecycleMethodNamesNewOnly =
5454
export const lifecycleMethodNamesLegacyOnly =
5555
lifecycleMethods.filter((mthd) => mthd.isLegacy).map(({name}) => name);
5656

57-
export const reduxStoreKey = 'lifecycleVisualizerStore';
58-
5957
const sessionStorageKey = '@@react-lifecycle-visualizer--persistent-state:';
6058
export const sessionReplayTimerDelayKey = sessionStorageKey + 'replayTimerDelay';
6159

src/redux/VisualizerProvider.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { createProvider } from 'react-redux';
3+
import { Provider } from 'react-redux';
44
import { applyMiddleware, createStore } from 'redux';
55
import thunk from 'redux-thunk';
66

7-
import * as constants from '../constants';
87
import * as actionCreators from './actionCreators';
98
import { reducer } from './reducer';
109

11-
const Provider = createProvider(constants.reduxStoreKey);
10+
// Context for the lifecycle-visualizer store
11+
export const LifecycleVisualizerContext = React.createContext();
1212

13-
const store = createStore(
13+
export const store = createStore(
1414
reducer,
1515
applyMiddleware(thunk)
1616
);
1717

1818
export const clearLog = () => store.dispatch(actionCreators.clearLog());
19-
// Store never changes, so we can safely export this bound function.
19+
// The store never changes, so we can safely export this bound function.
2020

2121
const VisualizerProvider = ({children}) => (
22-
<Provider store={store}>{children}</Provider>
22+
<Provider store={store} context={LifecycleVisualizerContext}>{children}</Provider>
2323
);
2424

2525
VisualizerProvider.propTypes = {

src/traceLifecycle.jsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Component } from 'react';
22
import hoistStatics from 'hoist-non-react-statics';
3-
import PropTypes from 'prop-types';
43

54
import * as constants from './constants';
65
import * as ActionCreators from './redux/actionCreators';
@@ -10,6 +9,7 @@ import { MConstructor, MShouldUpdate, MRender, MDidMount,
109
MDidUpdate, MWillUnmount, MSetState, MGetDerivedState, MGetSnapshot,
1110
MWillMount, MWillReceiveProps, MWillUpdate,
1211
MUnsafeWillMount, MUnsafeWillReceiveProps, MUnsafeWillUpdate} from './constants';
12+
import { store as lifecycleVisualizerStore } from './redux/VisualizerProvider';
1313

1414
const instanceIdCounters = {};
1515

@@ -189,26 +189,21 @@ export default function traceLifecycle(ComponentToTrace) {
189189
this.LifecyclePanel = WrappedLifecyclePanel;
190190

191191
this.trace = (methodName) => {
192-
this.context[constants.reduxStoreKey].dispatch(
193-
ActionCreators.trace(componentToTraceName, instanceId, methodName)
194-
);
195-
};
192+
// Just dispatch on lifecycleVisualizerStore directly, rather than introducing complexity by using context.
193+
lifecycleVisualizerStore.dispatch(
194+
ActionCreators.trace(componentToTraceName, instanceId, methodName)
195+
);
196+
};
196197
}
197198

198199
render() {
199200
return <TracedComponent LifecyclePanel={this.LifecyclePanel} trace={this.trace} {...this.props}/>;
200201
}
201202

202-
// Get store directly from context, to prevent introducing extra `Connect` component.
203-
static contextTypes = {
204-
...ComponentToTrace.contextTypes, // preserve contextTypes from ComponentToTrace
205-
[constants.reduxStoreKey]: PropTypes.object
206-
}
207-
208203
static displayName = `traceLifecycle(${componentToTraceName})`;
209204
}
210205

211-
// Removing the inappropriate methods is simpler than adding appropriate methods to prototype
206+
// Removing the inappropriate methods is simpler than adding appropriate methods to prototype.
212207
if (isLegacy) {
213208
delete TracedComponent.getDerivedStateFromProps;
214209
delete TracedComponent.prototype.getSnapshotBeforeUpdate;

0 commit comments

Comments
 (0)