-
Notifications
You must be signed in to change notification settings - Fork 40
Add setup function, to wrap componentDidMount #10
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm generally 🆗 on this even though I have a strange feeling both setup
and teardown
should be included for some vaguely aesthetic reason
I am partial to agree with @tippenein, but I am also ok with waiting til we actually hit a use case as well. Doing this with the |
Actually, If we're going to make these fields optional, |
I think it's required - what would the initial state be otherwise? |
empty state. stateless components are required to do |
@tippenein you can do |
sure. Why though? Isn't that the base case s/t you don't need it to create a valid stateless component? |
But what if the state type isn't |
There's no way with |
I see! because |
I've been thinking about this for a bit.. I think there are a couple different things going on in these PRs so I've grouped them separately below: Lifecycle hooksWe probably need at least one more lifecycle function in even the most basic stateful component. Here are the lifecycle functions in the reducer branch: initialState :: props -> state
receiveProps :: props -> state -> state
didMount :: props -> state -> action
didUpdate :: props -> state -> action
reducer :: Reducer eff action state
render :: Render eff props state action
displayName :: String The reducer/action bit is stylistic, but even if we replace
|
ORRRR! (I typed all that and thought of something different while skimming it once more 😆) We could do this: With that single "lifecycle" function we can drop example :: R.ReactComponent ExampleProps
example = R.react { receiveProps, render }
where
receiveProps props state _setState = pure { counter: maybe 0 _.counter state }
render props state setState =
R.button { onClick: mkEffFn1 \_ -> do
setState \s -> s { counter = s.counter + 1 }
}
[ R.text label ] Edit: different lifecycle option -- this still includes the Edit 2: |
Nevermind.. here's a slightly less trivial version: profile :: R.ReactComponent { userId :: UserId }
profile = R.react { receiveProps, render }
where
receiveProps props Nothing setState = do
......
receiveProps props (Just state) setState = when (props.userId /= state.userId) do
............in hindsight this is not great
render _ { Loading } _ = R.text "loading..."
render _ { Error _ } _ = R.text "oops..."
render _ { Ready user } _ = R.text user.name How about profile :: R.ReactComponent { userId :: UserId }
profile = R.react { receiveProps, render }
where
initialState { userId } = { userId, Loading } -- or Empty? Do we
-- need to call receiveProps anyway
-- for mount side effects?
receiveProps props state setState = when (props.userId /= state.userId) do
............
render _ { Loading } _ = R.text "loading..."
render _ { Error _ } _ = R.text "oops..."
render _ { Ready user } _ = R.text user.name Maybe profile :: R.ReactComponent { userId :: UserId }
profile = R.react { receiveProps, render }
where
initialState = { userId: Nothing, user: Loading }
receiveProps props state setState =
when (Just props.userId /= state.userId) do
runAff_ (\result ->
setState \latestState ->
if latestState.userId /= props.userId
then latestState
else case result of
Left err -> Error err
Right user -> Ready user
-- well that was fun.. and probably has a bug,
-- and doesn't cancel unused requests..
-- maybe the only way to keep UI logic reasonable
-- is to bake in FRP 🙂
) (fetchUser props.userId)
render _ { Loading } _ = R.text "loading..."
render _ { Error _ } _ = R.text "oops..."
render _ { Ready user } _ = R.text user.name (Is passing Sorry, I think this got a little off topic.. I should go to bed |
Add component function, and example
Counter example (no webpack)
README.md
Outdated
@@ -42,6 +42,7 @@ type ExampleState = | |||
example :: R.ReactComponent ExampleProps | |||
example = R.react | |||
{ initialState: \_ -> { counter: 0 } | |||
, setup: \_ _ _ -> pure unit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be receiveProps
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks!
It would be nice to get package-lock.json to not appear in diffs but I've not been able to get that to work. (using |
No description provided.