Skip to content

Commit 3514a4c

Browse files
authored
Provide previous props/state to didUpdate (#70)
Closes #68 `shouldUpdate` has similar props and I've found them confusing as nothing names them as "next" or "prev", so I changed both to receive a record which at least has the property names visible. What do you think, @akheron?
1 parent e84d1e3 commit 3514a4c

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/React/Basic.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@ exports.createComponent = (function() {
1616
return self;
1717
}
1818

19-
function shouldComponentUpdate(nextProps, nextState) {
20-
var shouldUpdate = this.$$spec.shouldUpdate;
21-
return shouldUpdate === undefined
22-
? true
23-
: shouldUpdate(this.toSelf())(nextProps.$$props)(
24-
nextState === null ? null : nextState.$$state
25-
);
26-
}
27-
2819
function componentDidMount() {
2920
var didMount = this.$$spec.didMount;
3021
if (didMount !== undefined) {
3122
didMount(this.toSelf())();
3223
}
3324
}
3425

35-
function componentDidUpdate() {
26+
function shouldComponentUpdate(nextProps, nextState) {
27+
var shouldUpdate = this.$$spec.shouldUpdate;
28+
return shouldUpdate === undefined
29+
? true
30+
: shouldUpdate(this.toSelf())({
31+
nextProps: nextProps.$$props,
32+
nextState: nextState === null ? null : nextState.$$state
33+
});
34+
}
35+
36+
function componentDidUpdate(prevProps, prevState) {
3637
var didUpdate = this.$$spec.didUpdate;
3738
if (didUpdate !== undefined) {
38-
didUpdate(this.toSelf())();
39+
didUpdate(this.toSelf())({
40+
prevProps: prevProps.$$props,
41+
prevState: prevState === null ? null : prevState.$$state
42+
})();
3943
}
4044
}
4145

@@ -136,8 +140,8 @@ exports.make = function(_unionDict) {
136140
initialState: $$spec.initialState,
137141
update: $$spec.update,
138142
render: $$spec.render,
139-
shouldUpdate: $$spec.shouldUpdate,
140143
didMount: $$spec.didMount,
144+
shouldUpdate: $$spec.shouldUpdate,
141145
didUpdate: $$spec.didUpdate,
142146
willUnmount: $$spec.willUnmount
143147
};
@@ -187,8 +191,8 @@ exports.toReactComponent = function(_unionDict) {
187191
initialState: $$spec.initialState,
188192
update: $$spec.update,
189193
render: $$spec.render,
190-
shouldUpdate: $$spec.shouldUpdate,
191194
didMount: $$spec.didMount,
195+
shouldUpdate: $$spec.shouldUpdate,
192196
didUpdate: $$spec.didUpdate,
193197
willUnmount: $$spec.willUnmount
194198
};

src/React/Basic.purs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ import Type.Row (class Union)
5858
-- | - Side effects requested are only invoked _after_ any corrosponding state update has completed its render cycle and the changes have been applied. This means it is safe to interact with the DOM in a side effect, for example.
5959
-- | - `render`
6060
-- | - Takes a current snapshot of the component (`Self`) and converts it to renderable `JSX`.
61-
-- | - `shouldUpdate`
62-
-- | - Can be useful for performance optimizations. Rarely necessary.
6361
-- | - `didMount`
6462
-- | - The React component's `componentDidMount` lifecycle. Useful for initiating an action on first mount, such as fetching data from a server.
63+
-- | - `shouldUpdate`
64+
-- | - Can be useful for performance optimizations. Rarely necessary.
6565
-- | - `didUpdate`
6666
-- | - The React component's `componentDidUpdate` lifecycle. Rarely necessary.
6767
-- | - `willUnmount`
@@ -110,9 +110,9 @@ type ComponentSpec props state action =
110110
( initialState :: state
111111
, update :: Self props state action -> action -> StateUpdate props state action
112112
, render :: Self props state action -> JSX
113-
, shouldUpdate :: Self props state action -> props -> state -> Boolean
114113
, didMount :: Self props state action -> Effect Unit
115-
, didUpdate :: Self props state action -> Effect Unit
114+
, shouldUpdate :: Self props state action -> { nextProps :: props, nextState :: state } -> Boolean
115+
, didUpdate :: Self props state action -> { prevProps :: props, prevState :: state } -> Effect Unit
116116
, willUnmount :: Self props state action -> Effect Unit
117117
)
118118

src/React/Basic/Compat.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ component { displayName, initialState, receiveProps, render } =
2828
toReactComponent identity (createComponent displayName)
2929
{ initialState: initialState
3030
, didMount: receiveProps <<< selfToLegacySelf
31-
, didUpdate: receiveProps <<< selfToLegacySelf
31+
, didUpdate: const <<< receiveProps <<< selfToLegacySelf
3232
, update: \self stateUpdate -> Update (stateUpdate self.state)
3333
, render: render <<< selfToLegacySelf
3434
}

0 commit comments

Comments
 (0)