Skip to content

Use "componentDidUpdate" #51

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 1 commit into from
Aug 16, 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
4 changes: 2 additions & 2 deletions examples/component/src/Container.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ component = React.stateless { displayName: "Container", render }
render _ =
R.div
{ children:
[ React.element ToggleButton.component { on: true }
, React.element ToggleButton.component { on: false }
[ React.element ToggleButton.component { label: "A" }
, React.element ToggleButton.component { label: "B" }
]
}
21 changes: 14 additions & 7 deletions examples/component/src/ToggleButton.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ import React.Basic.DOM as R
import React.Basic.Events as Events

type Props =
{ on :: Boolean
{ label :: String
}

component :: React.Component Props
component = React.component { displayName: "ToggleButton", initialState, receiveProps, render }
where
initialState =
{ on: false }
{ on: false
}

receiveProps { props, setState } =
setState _ { on = props.on }
receiveProps _ =
pure unit

render { state, setState } =
render { props, state, setState } =
R.button
{ onClick: Events.handler_ (setState \s -> s { on = not s.on })
, children: [ R.text if state.on then "On" else "Off" ]
{ onClick: Events.handler_ do
setState \s -> s { on = not s.on }
, children:
[ R.text props.label
, R.text if state.on
then " On"
else " Off"
]
}
12 changes: 5 additions & 7 deletions src/React/Basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ exports.component_ = function(spec) {
state: this.state,
setState: this._setState,
setStateThen: this._setState,
instance_: this,
instance_: this
});
};

Component.prototype.componentWillReceiveProps = function componentWillReceiveProps(
newProps
) {
Component.prototype.componentDidUpdate = function componentDidUpdate() {
spec.receiveProps({
isFirstMount: false,
props: newProps,
props: this.props,
state: this.state,
setState: this._setState,
setStateThen: this._setState,
instance_: this,
instance_: this
});
};

Expand All @@ -44,7 +42,7 @@ exports.component_ = function(spec) {
state: this.state,
setState: this._setState,
setStateThen: this._setState,
instance_: this,
instance_: this
});
};

Expand Down