Skip to content

Provide previous props/state to didUpdate #70

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

Merged
merged 2 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/React/Basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ exports.createComponent = (function() {
return self;
}

function shouldComponentUpdate(nextProps, nextState) {
var shouldUpdate = this.$$spec.shouldUpdate;
return shouldUpdate === undefined
? true
: shouldUpdate(this.toSelf())(nextProps.$$props)(
nextState === null ? null : nextState.$$state
);
}

function componentDidMount() {
var didMount = this.$$spec.didMount;
if (didMount !== undefined) {
didMount(this.toSelf())();
}
}

function componentDidUpdate() {
function shouldComponentUpdate(nextProps, nextState) {
var shouldUpdate = this.$$spec.shouldUpdate;
return shouldUpdate === undefined
? true
: shouldUpdate(this.toSelf())({
nextProps: nextProps.$$props,
nextState: nextState === null ? null : nextState.$$state
});
}

function componentDidUpdate(prevProps, prevState) {
var didUpdate = this.$$spec.didUpdate;
if (didUpdate !== undefined) {
didUpdate(this.toSelf())();
didUpdate(this.toSelf())({
prevProps: prevProps.$$props,
prevState: prevState === null ? null : prevState.$$state
})();
}
}

Expand Down Expand Up @@ -136,8 +140,8 @@ exports.make = function(_unionDict) {
initialState: $$spec.initialState,
update: $$spec.update,
render: $$spec.render,
shouldUpdate: $$spec.shouldUpdate,
didMount: $$spec.didMount,
shouldUpdate: $$spec.shouldUpdate,
didUpdate: $$spec.didUpdate,
willUnmount: $$spec.willUnmount
};
Expand Down Expand Up @@ -187,8 +191,8 @@ exports.toReactComponent = function(_unionDict) {
initialState: $$spec.initialState,
update: $$spec.update,
render: $$spec.render,
shouldUpdate: $$spec.shouldUpdate,
didMount: $$spec.didMount,
shouldUpdate: $$spec.shouldUpdate,
didUpdate: $$spec.didUpdate,
willUnmount: $$spec.willUnmount
};
Expand Down
8 changes: 4 additions & 4 deletions src/React/Basic.purs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ import Type.Row (class Union)
-- | - 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.
-- | - `render`
-- | - Takes a current snapshot of the component (`Self`) and converts it to renderable `JSX`.
-- | - `shouldUpdate`
-- | - Can be useful for performance optimizations. Rarely necessary.
-- | - `didMount`
-- | - The React component's `componentDidMount` lifecycle. Useful for initiating an action on first mount, such as fetching data from a server.
-- | - `shouldUpdate`
-- | - Can be useful for performance optimizations. Rarely necessary.
-- | - `didUpdate`
-- | - The React component's `componentDidUpdate` lifecycle. Rarely necessary.
-- | - `willUnmount`
Expand Down Expand Up @@ -110,9 +110,9 @@ type ComponentSpec props state action =
( initialState :: state
, update :: Self props state action -> action -> StateUpdate props state action
, render :: Self props state action -> JSX
, shouldUpdate :: Self props state action -> props -> state -> Boolean
, didMount :: Self props state action -> Effect Unit
, didUpdate :: Self props state action -> Effect Unit
, shouldUpdate :: Self props state action -> { nextProps :: props, nextState :: state } -> Boolean
, didUpdate :: Self props state action -> { prevProps :: props, prevState :: state } -> Effect Unit
, willUnmount :: Self props state action -> Effect Unit
)

Expand Down
2 changes: 1 addition & 1 deletion src/React/Basic/Compat.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ component { displayName, initialState, receiveProps, render } =
toReactComponent identity (createComponent displayName)
{ initialState: initialState
, didMount: receiveProps <<< selfToLegacySelf
, didUpdate: receiveProps <<< selfToLegacySelf
, didUpdate: const <<< receiveProps <<< selfToLegacySelf
, update: \self stateUpdate -> Update (stateUpdate self.state)
, render: render <<< selfToLegacySelf
}
Expand Down