Skip to content
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

Replace componentWillReceiveProps with componentDidUpdate #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -71,7 +71,8 @@ autocomplete( event ) {
```javascript
@keydown
class MyComponent extends React.Component {
componentWillReceiveProps( { keydown } ) {
componentDidUpdate() {
const { keydown } = this.props;
if ( keydown.event ) {
// inspect the keydown event and decide what to do
console.log( keydown.event.which );
Expand Down Expand Up @@ -115,7 +116,7 @@ class MyComponent extends React.Component {

class MyOtherComponent extends React.Component {
...
@keydownScoped( 'enter' ) // inspects nextProps.keydown.event in componentWillReceiveProps behind the scenes
@keydownScoped( 'enter' ) // inspects this.props.keydown.event in componentDidUpdate behind the scenes
submit() {
// submit
}
Expand Down
4 changes: 2 additions & 2 deletions example/src/app/class_decorator/code.js
Expand Up @@ -16,8 +16,8 @@ class MyComponent extends React.Component {
};
}

componentWillReceiveProps( nextProps ) {
const { keydown: { event } } = nextProps;
componentDidUpdate() {
const { keydown: { event } } = this.props;
if ( event ) {
this.setState( { key: event.which } );
}
Expand Down
3 changes: 2 additions & 1 deletion example/src/app/class_decorator/index.js
Expand Up @@ -11,7 +11,8 @@ class MyComponent extends React.Component {
};
}

componentWillReceiveProps( { keydown: { event } } ) {
componentDidUpdate() {
const { keydown: { event } } = this.props;
if ( event ) {
this.setState( { key: event.which } );
}
Expand Down
10 changes: 5 additions & 5 deletions src/decorators/method_decorator_scoped.js
Expand Up @@ -16,7 +16,7 @@ import parseKeys from '../lib/parse_keys';
* @return {object} The method's descriptor object
*/
function methodWrapperScoped( { target, descriptor, keys } ) {
const { componentWillReceiveProps } = target;
const { componentDidUpdate } = target;
const fn = descriptor.value;
if ( !keys ) {
console.warn( `${fn}: keydownScoped requires one or more keys` );
Expand Down Expand Up @@ -46,15 +46,15 @@ function methodWrapperScoped( { target, descriptor, keys } ) {
// from the wrapped/scoped component up the view hierarchy. if new keydown
// event has arrived and the key codes match what was specified in the
// decorator, call the wrapped method.
target.componentWillReceiveProps = function( nextProps, ...args ) {
const { keydown: keydownNext } = nextProps;
const { keydown: keydownThis } = this.props;
target.componentDidUpdate = function( prevProps, ...args ) {
const { keydown: keydownNext } = this.props;
const { keydown: keydownThis } = prevProps;

if ( _shouldTrigger( keydownThis, keydownNext ) ) {
return fn.call( this, keydownNext.event );
}

if ( componentWillReceiveProps ) return componentWillReceiveProps.call( this, nextProps, ...args );
if ( componentDidUpdate ) return componentDidUpdate.call( this, prevProps, ...args );
};
}

Expand Down