Skip to content

Commit 3384ff6

Browse files
committed
feat(React-16): add support for React 16
1 parent e023a15 commit 3384ff6

File tree

6 files changed

+962
-715
lines changed

6 files changed

+962
-715
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
npm-debug.log*
3+
yarn-error.log
34
lib
45
coverage
56
docs/static

package.json

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,70 @@
88
},
99
"dependencies": {
1010
"invariant": "^2.2.2",
11-
"prop-types": "^15.5.10"
11+
"prop-types": "^15.6.0"
1212
},
1313
"devDependencies": {
1414
"@researchgate/babel-preset-rg": "^1.0.1",
15-
"@researchgate/eslint-config-rg-react": "^1.0.0",
16-
"@storybook/addon-actions": "^3.2.0",
17-
"@storybook/addon-options": "^3.2.4",
18-
"@storybook/react": "^3.2.8",
15+
"@researchgate/eslint-config-rg-react": "^1.0.1",
16+
"@storybook/addon-actions": "^3.2.16",
17+
"@storybook/addon-options": "^3.2.16",
18+
"@storybook/react": "^3.2.16",
1919
"babel-cli": "^6.24.1",
20-
"babel-eslint": "^8.0.0",
21-
"babel-jest": "^21.0.0",
20+
"babel-eslint": "^8.0.2",
21+
"babel-jest": "^21.2.0",
2222
"babel-polyfill": "^6.26.0",
2323
"conventional-github-releaser": "^2.0.0",
24-
"cross-env": "^5.0.5",
24+
"cross-env": "^5.1.1",
2525
"css-loader": "^0.28.4",
26-
"eslint": "^4.6.1",
27-
"eslint-config-prettier": "^2.4.0",
28-
"eslint-plugin-import": "^2.7.0",
26+
"eslint": "^4.11.0",
27+
"eslint-config-prettier": "^2.8.0",
28+
"eslint-plugin-import": "^2.8.0",
2929
"eslint-plugin-prettier": "^2.3.0",
30-
"eslint-plugin-react": "^7.3.0",
30+
"eslint-plugin-react": "^7.5.1",
3131
"husky": "^0.14.3",
32-
"intersection-observer": "^0.4.2",
33-
"jest": "^21.0.2",
32+
"intersection-observer": "^0.4.3",
33+
"jest": "^21.2.1",
3434
"lint-staged": "^5.0.0",
35-
"prettier": "^1.6.1",
35+
"prettier": "^1.8.2",
36+
"raf": "^3.4.0",
3637
"raw-loader": "^1.0.0-beta.0",
37-
"react": "^15.4.0",
38-
"react-dom": "^15.4.0",
39-
"react-test-renderer": "^15.6.1",
38+
"react": "^16.1.1",
39+
"react-dom": "^16.1.1",
40+
"react-test-renderer": "^16.1.1",
4041
"rimraf": "^2.6.1",
4142
"standard-version": "^4.2.0",
42-
"storybook-readme": "^3.0.6",
43+
"storybook-readme": "^3.1.1",
4344
"style-loader": "^0.19.0",
4445
"validate-commit-msg": "^2.14.0"
4546
},
46-
"files": [
47-
"lib"
48-
],
47+
"files": ["lib"],
4948
"homepage": "https://github.com/researchgate/react-intersection-observer#readme",
50-
"keywords": [
51-
"Intersection",
52-
"Observer",
53-
"react",
54-
"component",
55-
"viewport",
56-
"visible",
57-
"invisible",
58-
"researchgate"
59-
],
49+
"keywords": ["Intersection", "Observer", "react", "component", "viewport", "visible", "invisible", "researchgate"],
6050
"license": "MIT",
6151
"lint-staged": {
62-
"{src,docs/docs}/**/*.js": [
63-
"eslint --fix",
64-
"git add"
65-
]
52+
"{src,docs/docs}/**/*.js": ["eslint --fix", "git add"]
6653
},
6754
"main": "lib/js/index.js",
6855
"module": "lib/es/index.js",
6956
"peerDependencies": {
70-
"react": "^15.4.0",
71-
"react-dom": "^15.4.0"
57+
"react": "^15.4.0 | ^16.1.1",
58+
"react-dom": "^15.4.0 | ^16.1.1"
7259
},
7360
"repository": {
7461
"type": "git",
7562
"url": "https://github.com/researchgate/react-intersection-observer"
7663
},
7764
"jest": {
7865
"rootDir": "src",
79-
"testMatch": [
80-
"**/__tests__/**/*.spec.js"
81-
]
66+
"testMatch": ["**/__tests__/**/*.spec.js"],
67+
"setupFiles": ["raf/polyfill"]
8268
},
8369
"scripts": {
8470
"build": "yarn build:js && yarn build:es",
85-
"build:js": "cross-env BABEL_ENV=production BABEL_OUTPUT=cjs babel src --out-dir lib/js --ignore __tests__ --copy-files",
86-
"build:es": "cross-env BABEL_ENV=production BABEL_OUTPUT=esm babel src --out-dir lib/es --ignore __tests__ --copy-files",
71+
"build:js":
72+
"cross-env BABEL_ENV=production BABEL_OUTPUT=cjs babel src --out-dir lib/js --ignore __tests__ --copy-files",
73+
"build:es":
74+
"cross-env BABEL_ENV=production BABEL_OUTPUT=esm babel src --out-dir lib/es --ignore __tests__ --copy-files",
8775
"build:storybook": "build-storybook --output-dir docs",
8876
"clear": "rimraf ./lib",
8977
"commitmsg": "validate-commit-msg",

src/IntersectionObserver.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export default class IntersectionObserver extends React.Component {
108108
if (process.env.NODE_ENV !== 'production') {
109109
invariant(
110110
'isIntersecting' in event,
111-
'`IntersectionObserverEntry` missing the property `isIntersecting`. When using the prop `onlyOnce` this needs to exists in IntersectionObserverEntry.',
111+
"onlyOnce requires isIntersecting to exists in IntersectionObserverEntry's prototype. Either your browser or your polyfill lacks support.",
112112
);
113113
}
114114
if (event.isIntersecting) {
@@ -147,10 +147,10 @@ export default class IntersectionObserver extends React.Component {
147147

148148
componentDidMount() {
149149
// eslint-disable-next-line no-undef
150-
if (process.env.NODE_ENV !== 'production') {
150+
if (process.env.NODE_ENV !== 'production' && parseInt(React.version, 10) < 16) {
151151
invariant(
152152
this.target,
153-
'Could not save a ref to the component. Make sure the provided children prop is not a StatelessComponent',
153+
'Stateless function components cannot be given refs. Attempts to access this ref will fail.',
154154
);
155155
}
156156
if (!this.props.disabled) {

src/__tests__/IntersectionObserver.spec.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ afterEach(() => {
3434
});
3535

3636
test('throws when the property children is not an only child', () => {
37+
global.spyOn(console, 'error');
3738
const component = (
3839
<IntersectionObserver onChange={noop}>
3940
<span />
@@ -43,17 +44,19 @@ test('throws when the property children is not an only child', () => {
4344
expect(() => renderer.create(component)).toThrowErrorMatchingSnapshot();
4445
});
4546

46-
test('throws on mount if children is StatelessComponent', () => {
47+
test('throws on mount if children is StatelessComponent in React 15', () => {
48+
global.spyOn(console, 'error');
49+
const { version } = React.version;
4750
const StatelessComponent = () => <span />;
4851
const component = (
4952
<IntersectionObserver onChange={noop}>
5053
<StatelessComponent />
5154
</IntersectionObserver>
5255
);
5356

54-
expect(() => renderer.create(component)).toThrow(
55-
'Could not save a ref to the component. Make sure the provided children prop is not a StatelessComponent',
56-
);
57+
React.version = '15.4.0';
58+
expect(() => renderer.create(component)).toThrowErrorMatchingSnapshot();
59+
React.version = version;
5760
});
5861

5962
test('should call ref callback of children', () => {
@@ -287,9 +290,7 @@ describe('handleChange', () => {
287290
});
288291
delete entry.isIntersecting;
289292

290-
expect(() => instance.handleChange(entry)).toThrow(
291-
'`IntersectionObserverEntry` missing the property `isIntersecting`. When using the prop `onlyOnce` this needs to exists in IntersectionObserverEntry.',
292-
);
293+
expect(() => instance.handleChange(entry)).toThrowErrorMatchingSnapshot();
293294
});
294295

295296
test('should unobserve with `onlyOnce` if `isIntersecting` is true', () => {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`handleChange should throw with \`onlyOnce\` if entry lacks \`isIntersecting\` 1`] = `"onlyOnce requires isIntersecting to exists in IntersectionObserverEntry's prototype. Either your browser or your polyfill lacks support."`;
4+
5+
exports[`throws on mount if children is StatelessComponent in React 15 1`] = `"Stateless function components cannot be given refs. Attempts to access this ref will fail."`;
6+
37
exports[`throws when the property children is not an only child 1`] = `"React.Children.only expected to receive a single React element child."`;

0 commit comments

Comments
 (0)