Skip to content

Commit 976159e

Browse files
committed
adding state as a function and deep cloning for instance props, adding componentWillReceiveProps lifecycle
1 parent a3ab4cd commit 976159e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

integration/preact/component_v2.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default function component(options = {}) {
3636
instanceProps,
3737
componentDidMount,
3838
componentWillUnmount,
39+
componentWillReceiveProps,
3940
...rest
4041
} = options;
4142
let { actions, watcher } = options;
@@ -46,8 +47,7 @@ export default function component(options = {}) {
4647
const setState = (newState) => this.setState({ ...newState });
4748
const getState = () => this.state;
4849
const _self = this;
49-
50-
this.state = state || {};
50+
this.state = state(props) || {};
5151
this.__store__ = Wrapper.__store__;
5252

5353
if (this.__store__) {
@@ -56,10 +56,11 @@ export default function component(options = {}) {
5656
}
5757

5858
const pluck = getProps(watcher);
59+
const instancePropsClone = {...instanceProps}
5960
// Attach handlers
6061
bindHandler.call(this, actions, store.action);
6162
bindHandler.call(this, rest, this.proxy);
62-
attachInstanceProps.call(this, instanceProps);
63+
attachInstanceProps.call(this, instancePropsClone);
6364

6465
let globalState = this.__store__ ? pluck(store ? store.getState() : {}) : {};
6566

@@ -102,6 +103,11 @@ export default function component(options = {}) {
102103
}
103104
componentWillUnmount && componentWillUnmount.call(null, this.mergeProps());
104105
}
106+
107+
this.componentWillReceiveProps = (nxtProps) => {
108+
componentWillReceiveProps && componentWillReceiveProps.call(null, nxtProps, this.mergeProps())
109+
}
110+
105111
this.render = (props) => {
106112
const view = template || InnerComponent;
107113
return h(view, this.mergeProps());

0 commit comments

Comments
 (0)