Skip to content
This repository was archived by the owner on Dec 23, 2019. It is now read-only.

Commit 085a653

Browse files
authored
v1.2.0 New Context API (#49)
* upgrade deps * migrate to new context api * version++ * update peer deps
1 parent 6f37d17 commit 085a653

8 files changed

Lines changed: 2541 additions & 3294 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.2.0
4+
5+
- Upgrade to the new Context API
6+
37
## 1.1.0
48

59
- Updated Action to set loading state to true when the action is triggered

package-lock.json

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

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@perchsecurity/perch-data",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Utilities for managing data. Inspired by react-apollo.",
55
"main": "lib/index.js",
66
"private": false,
@@ -27,32 +27,32 @@
2727
"homepage": "https://github.com/usePF/perch-data#readme",
2828
"peerDependencies": {
2929
"prop-types": "^15.0.0",
30-
"react": ">=15.0.0",
31-
"react-dom": ">=15.0.0"
30+
"react": ">=16.7.0",
31+
"react-dom": ">=16.7.0"
3232
},
3333
"devDependencies": {
3434
"babel-cli": "^6.26.0",
35-
"babel-eslint": "^8.2.2",
35+
"babel-eslint": "^8.2.6",
3636
"babel-plugin-transform-class-properties": "^6.24.1",
3737
"babel-plugin-transform-export-extensions": "^6.22.0",
3838
"babel-plugin-transform-object-rest-spread": "^6.26.0",
39-
"babel-preset-env": "^1.6.1",
39+
"babel-preset-env": "^1.7.0",
4040
"babel-preset-react": "^6.24.1",
4141
"eslint": "^4.19.1",
4242
"eslint-config-airbnb": "^16.1.0",
43-
"eslint-config-prettier": "^2.9.0",
44-
"eslint-plugin-import": "^2.9.0",
45-
"eslint-plugin-jsx-a11y": "^6.0.3",
46-
"eslint-plugin-react": "^7.7.0",
47-
"jest": "^22.4.3",
48-
"prettier": "^1.11.1",
49-
"prop-types": "^15.0.0",
50-
"react": "^15.0.0",
51-
"react-dom": "^15.0.0",
52-
"react-test-renderer": "^15.0.0"
43+
"eslint-config-prettier": "^2.10.0",
44+
"eslint-plugin-import": "^2.15.0",
45+
"eslint-plugin-jsx-a11y": "^6.1.2",
46+
"eslint-plugin-react": "^7.12.4",
47+
"jest": "^22.4.4",
48+
"prettier": "^1.16.1",
49+
"prop-types": "^15.6.2",
50+
"react": "^16.7.0",
51+
"react-dom": "^16.7.0",
52+
"react-test-renderer": "^16.7.0"
5353
},
5454
"dependencies": {
55-
"immutability-helper": "^2.6.6",
55+
"immutability-helper": "^2.9.0",
5656
"lodash.isequal": "^4.5.0",
5757
"store": "^2.0.12"
5858
}

src/Action.jsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from "react";
22
import PropTypes from "prop-types";
33

4+
import { StoreContext } from "./";
5+
46
class Action extends React.Component {
57
constructor(props, context) {
68
super(props, context);
@@ -41,6 +43,8 @@ class Action extends React.Component {
4143
}
4244
}
4345

46+
Action.contextType = StoreContext;
47+
4448
Action.propTypes = {
4549
action: PropTypes.func.isRequired,
4650
children: PropTypes.func.isRequired,
@@ -58,9 +62,4 @@ Action.defaultProps = {
5862
variables: {}
5963
};
6064

61-
Action.contextTypes = {
62-
store: PropTypes.object.isRequired,
63-
api: PropTypes.func
64-
};
65-
6665
export default Action;

src/Data.jsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import React from "react";
22
import PropTypes from "prop-types";
33
import isEqual from "lodash.isequal";
44

5+
import { StoreContext } from "./";
6+
57
class Data extends React.Component {
6-
constructor(props, context) {
7-
super(props, context);
8+
constructor(props) {
9+
super(props);
810
this.state = {
911
data: null,
1012
error: null,
@@ -99,6 +101,8 @@ class Data extends React.Component {
99101
}
100102
}
101103

104+
Data.contextType = StoreContext;
105+
102106
Data.propTypes = {
103107
action: PropTypes.func.isRequired,
104108
children: PropTypes.func.isRequired,
@@ -114,12 +118,4 @@ Data.defaultProps = {
114118
variables: {}
115119
};
116120

117-
Data.contextTypes = {
118-
api: PropTypes.func,
119-
store: PropTypes.shape({
120-
observeData: PropTypes.func,
121-
unobserveData: PropTypes.func
122-
}).isRequired
123-
};
124-
125121
export default Data;
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
import React from "react";
1+
import React, { createContext } from "react";
22
import PropTypes from "prop-types";
33
import { axiosStore } from "./";
44

55
const MINUTE = 1000 * 60;
66

7-
class StoreProvider extends React.Component {
7+
export const StoreContext = createContext({});
8+
9+
export class StoreProvider extends React.Component {
810
constructor(props) {
911
super(props);
10-
this.state = { poll: setInterval(this.collectGarbage, MINUTE) };
11-
}
12-
13-
getChildContext() {
14-
const { api, store, initialValues } = this.props;
12+
const { api, store, initialValues } = props;
1513
store.initializeStore(initialValues);
16-
return { api: api && axiosStore(api, store), store };
14+
this.state = {
15+
api: api && axiosStore(api, store), // eslint-disable-line
16+
poll: setInterval(this.collectGarbage, MINUTE),
17+
store
18+
};
1719
}
1820

1921
componentWillUnmount() {
20-
if (this.state.poll) clearInterval(this.state.poll);
22+
const { poll } = this.state;
23+
if (poll) clearInterval(poll);
2124
}
2225

2326
collectGarbage = () => {
24-
const { store } = this.props;
27+
const { store } = this.state;
2528
if (store && store.removeExpiredKeys) store.removeExpiredKeys();
2629
};
2730

2831
render() {
29-
return this.props.children;
32+
return (
33+
<StoreContext.Provider value={this.state}>
34+
{this.props.children}
35+
</StoreContext.Provider>
36+
);
3037
}
3138
}
3239

@@ -47,9 +54,4 @@ StoreProvider.defaultProps = {
4754
initialValues: null
4855
};
4956

50-
StoreProvider.childContextTypes = {
51-
api: PropTypes.func,
52-
store: PropTypes.object
53-
};
54-
55-
export default StoreProvider;
57+
export const StoreConsumer = StoreContext.Consumer;

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export withData from "./withData";
22
export * as cache from "./cache";
33
export axiosStore from "./axiosStore";
4-
export StoreProvider from "./StoreProvider";
4+
export * from "./StoreContext";
55
export Data from "./Data";
66
export Action from "./Action";

src/withData.jsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from "react";
2-
import PropTypes from "prop-types";
32
import update from "immutability-helper";
43

4+
import { StoreContext } from "./";
5+
56
function withData(actions) {
67
function enhance(WrappedComponent) {
78
class Enhanced extends React.Component {
@@ -162,12 +163,7 @@ function withData(actions) {
162163
}
163164
}
164165

165-
Enhanced.contextTypes = {
166-
store: PropTypes.shape({
167-
observeData: PropTypes.func,
168-
unobserveData: PropTypes.func
169-
}).isRequired
170-
};
166+
Enhanced.contextType = StoreContext;
171167

172168
return Enhanced;
173169
}

0 commit comments

Comments
 (0)