Skip to content

How to use deps #55

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

Closed
codingedgar opened this issue Dec 13, 2021 · 3 comments
Closed

How to use deps #55

codingedgar opened this issue Dec 13, 2021 · 3 comments

Comments

@codingedgar
Copy link

I'm sorry for the noob question, useEffect signature is:

useEffect ::
  forall deps.
  Eq deps =>
  deps ->
  Effect (Effect Unit) ->
  Hook (UseEffect deps) Unit
useEffect deps effect =
  unsafeHook do
    runEffectFn3 useEffect_ (mkFn2 eq) deps effect

in js I often use

useEffect (() => {
  // ...
  },
  [stringDependency, booleanDependency]
)

but useEffect has a homogenous deps, how can I have a deps list of different types?

I know this is more of a question about PureScript, but I think this is the best place to make this particular question and leave it for newcomers.

@pete-murphy
Copy link
Member

pete-murphy commented Dec 13, 2021

The deps in that signature isn't necessarily homogeneous, but polymorphic. So any deps can be passed as long as there is an Eq instance for that type. This is different from JS where you must provide dependencies in a tuple.

If you want a heterogeneous collection in PureScript, you could use a tuple or a record (which both have an Eq instance if the types they contain have Eq instances):

Hooks.useEffect { stringDependency, booleanDependency } do
  -- ..

or

Hooks.useEffect (stringDependency /\ booleanDependency) do
  -- ..

@codingedgar
Copy link
Author

Thanks a lot! ✨

@maddie927
Copy link
Member

Perfect, thanks @ptrfrncsmrph 🙂

I'm actually working on docs right now! I swear!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants