Skip to content
This repository has been archived by the owner on Jul 11, 2019. It is now read-only.

Commit

Permalink
[core] React 16.3.0 support (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed May 27, 2018
1 parent 3802bd4 commit a8fd86d
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .size-limit
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
name: 'The entry point',
webpack: true,
path: 'dist/react-event-listener.cjs.js',
limit: '2.13 KB'
limit: '2 KB'
}
]
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.42",
"fbjs": "^0.8.16",
"prop-types": "^15.6.0",
"warning": "^3.0.0"
},
"peerDependencies": {
"react": "^15.3.0 || ^16.0.0"

This comment has been minimized.

Copy link
@bhovhannes

bhovhannes Jun 2, 2018

Hey,
I've just installed v0.6.0 of react-event-listener and saw the warning regarding required peer dependency react ^16.3.0.
React.PureComponent exists in 15.4.0 version of React as well, so this line can be safely reverted to read: react: "^15.4.0 || ^16.0.0". That way there will be no warnings any more.
What do you think?

This comment has been minimized.

Copy link
@oliviertassinari

oliviertassinari Jun 2, 2018

Author Owner

We could, but I'm not interested in supporting older versions of React. If you really need to work with an older version, use v0.5.9.

"react": "^16.3.0"
}
}
32 changes: 8 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import shallowEqual from 'fbjs/lib/shallowEqual';
import warning from 'warning';
import { passiveOption } from './supports';

Expand Down Expand Up @@ -71,37 +70,22 @@ export function withOptions(handler, options) {
};
}

class EventListener extends React.Component {
class EventListener extends React.PureComponent {
componentDidMount() {
this.addListeners();
}

shouldComponentUpdate(nextProps) {
return !shallowEqual(this.props, nextProps);
}

componentWillUpdate() {
this.removeListeners();
}

componentDidUpdate() {
this.addListeners();
}

componentWillUnmount() {
this.removeListeners();
this.applyListeners(on);
}

addListeners() {
componentDidUpdate(prevProps) {
this.applyListeners(off, prevProps);
this.applyListeners(on);
}

removeListeners() {
componentWillUnmount() {
this.applyListeners(off);
}

applyListeners(onOrOff) {
const { target } = this.props;
applyListeners(onOrOff, props = this.props) {
const { target } = props;

if (target) {
let element = target;
Expand All @@ -110,7 +94,7 @@ class EventListener extends React.Component {
element = window[target];
}

forEachListener(this.props, onOrOff.bind(null, element));
forEachListener(props, onOrOff.bind(null, element));
}
}

Expand Down
Loading

0 comments on commit a8fd86d

Please sign in to comment.