-
Notifications
You must be signed in to change notification settings - Fork 33
Question: fetching initial data and putting it to state #32
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
Comments
Hello! (I edited your post just to add syntax highlighting) Your code seems fine to me. You're using that mkA :: Component Unit
mkA = do
component "A" \props -> React.do
foo <- suspend (runExceptT fetchInitialData)
s /\ setS <- useState foo
pure $ R.text "b" But it's a beta feature that isn't well documented yet (I'm working on it, but also it's still beta in React itself). As for I would also recommend against replacing Ok, If any of that is confusing or doesn't fit your use case please let me know. I'm working on updated documentation/guides and want to make sure it's as clear and useful as possible. 🙂 |
Hello. Thank you very much for your detailed answers. Sorry, I did not put an appropriate example where it would be clear, that the fetched data should be put in the state, so that later, clicks and other events could mutate that state. For example, when deleting a list item, I would modify the state, which holds that list, instead refetching the list again. As for mkA :: Component Unit
mkA = do
component "A" \props -> React.do
foo /\ setFoo <- useState ([] :: Array String)
React.useEffect unit do
launchAff_ $
runExceptT Api.getFoo >>= case _ of
Left e -> liftEffect $ log $ "err.."
Right r -> liftEffect $ setFoo (const r)
pure $ pure unit
pure
$ case foo of
[] -> R.text "..."
_ -> R.text $ show foo
There are a few edge cases where this is necessary, but in the vast majority of cases this is a bug |
Yes, there's usually a dependency to track. In your example there isn't, but if you ever added one there would be two potential bugs:
|
Thank you. |
Hi, I want to fetch some data and put it in the state. With
useAff
I would do something like this:My question is: How could I do the same in one component? I was thinking of using
useEffectOnce
withlaunchAff_
? Or is this not recommended?Also, the comment of
useEffectOnce
function says:I don't understand why is
useEffect
preferable overuseEffectOnce
, when I want the effect to be performed only once? Isn'tuseEffectOnce
's purpose just that, so that the user wouldn't need to pass a dummy param likeunit
or[]
as an argument?Thanks!
The text was updated successfully, but these errors were encountered: