Skip to content

Commit

Permalink
Use "componentDidUpdate" (#51)
Browse files Browse the repository at this point in the history
...instead of "componentWillReceiveProps"
  • Loading branch information
spicydonuts authored Aug 16, 2018
1 parent 02294d0 commit 5f9a0cc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
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

0 comments on commit 5f9a0cc

Please sign in to comment.