-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add willReceiveState and willReceiveProps lifecycle methods to Component and JSXComponent respectively #270
Changes from all commits
a0da0bd
9ae5c9b
e792273
add347f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,12 +28,21 @@ import { EventEmitter, EventHandler } from 'metal-events'; | |
* created() { | ||
* } | ||
* | ||
* willRender() { | ||
* } | ||
* | ||
* rendered() { | ||
* } | ||
* | ||
* willAttach() { | ||
* } | ||
* | ||
* attached() { | ||
* } | ||
* | ||
* willDetach() { | ||
* } | ||
* | ||
* detached() { | ||
* } | ||
* | ||
|
@@ -125,6 +134,7 @@ class Component extends EventEmitter { | |
this.setUpDataManager_(); | ||
this.setUpSyncUpdates_(); | ||
|
||
this.on('stateWillChange', this.handleStateWillChange_); | ||
this.on('stateChanged', this.handleComponentStateChanged_); | ||
this.on('eventsChanged', this.onEventsChanged_); | ||
this.addListenersFromObj_(this.dataManager_.get(this, 'events')); | ||
|
@@ -172,6 +182,8 @@ class Component extends EventEmitter { | |
*/ | ||
attach(opt_parentElement, opt_siblingElement) { | ||
if (!this.inDocument) { | ||
this.emit('willAttach'); | ||
this.willAttach(); | ||
this.attachElement(opt_parentElement, opt_siblingElement); | ||
this.inDocument = true; | ||
this.attachData_ = { | ||
|
@@ -238,6 +250,8 @@ class Component extends EventEmitter { | |
*/ | ||
detach() { | ||
if (this.inDocument) { | ||
this.emit('willDetach'); | ||
this.willDetach(); | ||
if (this.element && this.element.parentNode) { | ||
this.element.parentNode.removeChild(this.element); | ||
} | ||
|
@@ -377,6 +391,18 @@ class Component extends EventEmitter { | |
}); | ||
} | ||
|
||
/** | ||
* Fires before state batch changes. Provides hook point for modifying | ||
* state. | ||
* @param {Event} event | ||
* @protected | ||
*/ | ||
handleStateWillChange_(event) { | ||
if (this.willReceiveState) { | ||
this.willReceiveState(event.changes); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if this component has sync updates enabled. | ||
* @return {boolean} | ||
|
@@ -501,6 +527,9 @@ class Component extends EventEmitter { | |
* be called manually later to actually attach it to the dom. | ||
*/ | ||
renderComponent(opt_parentElement) { | ||
const firstRender = !this.hasRendererRendered_; | ||
this.emit('willRender', firstRender); | ||
this.willRender(firstRender); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on componentWillUpdate(), the signature for this method should be: componentWillUpdate(nextProps, nextState) Furthermore:
Finally, do we have the last note in mind?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I update this in the new pull. I changed the name to |
||
if (!this.hasRendererRendered_) { | ||
if (!this.serverSide_ && window.__METAL_DEV_TOOLS_HOOK__) { | ||
window.__METAL_DEV_TOOLS_HOOK__(this); | ||
|
@@ -650,6 +679,23 @@ class Component extends EventEmitter { | |
validatorEventsFn_(val) { | ||
return !isDefAndNotNull(val) || isObject(val); | ||
} | ||
|
||
/** | ||
* Lifecycle. Fires before the component has been attached to the DOM. | ||
*/ | ||
willAttach() {} | ||
|
||
/** | ||
* Lifecycle. Fires before component is detached from the DOM. | ||
*/ | ||
willDetach() {} | ||
|
||
/** | ||
* Lifecycle. Fires whenever the component is about to render. | ||
* @param {boolean} firstRender Flag indicating if this will be the | ||
* component's first render. | ||
*/ | ||
willRender() {} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Robert-Frampton, based on componentWillMount():
Can we assume the same with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jbalsas
So an area where Metal is already different than React is that our
render
actually happens beforeattach
. This is how the lifecycle is ordered on first render.created > willRender > rendered > willAttach > attached > willDetach > detached > disposed
I think the only way we can mirror React completely is to do a breaking change and change the order so that attachment happens before render, but obviously we can't do that for
2.14
. Although we could movewillAttach
only, but it feels weird to me.created > willAttach > willRender > rendered > attached > willDetach > detached > disposed
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine for now... I think we are entitled to some little quirks of our own ;)
I think we just need to improve our Component Lifecycle documentation so everything is clear :)