Skip to content

useEffectAlways does not always run #25

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
alextes opened this issue Apr 25, 2020 · 1 comment · Fixed by #26
Closed

useEffectAlways does not always run #25

alextes opened this issue Apr 25, 2020 · 1 comment · Fixed by #26

Comments

@alextes
Copy link

alextes commented Apr 25, 2020

In the following example based on the counter example:

module ReactExample where

import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console as Console
import Effect.Exception (throw)
import React.Basic.DOM (render)
import React.Basic.DOM as R
import React.Basic.Events (handler_)
import React.Basic.Hooks (Component, component, fragment, useEffect, useEffectAlways, useState, (/\))
import React.Basic.Hooks as React
import Web.DOM.NonElementParentNode (getElementById)
import Web.HTML (window)
import Web.HTML.HTMLDocument (toNonElementParentNode)
import Web.HTML.Window (document)

mkExample :: Component Unit
mkExample = do
  component "Counter" \_ -> React.do
    counter /\ setCounter <- useState 0
    useEffect counter $ Console.log ("useEffect " <> show counter) *> pure mempty
    useEffectAlways $ Console.log ("useEffectAlways " <> show counter) *> pure mempty
    pure
      $ fragment
          [ R.button
              { onClick: handler_ $ setCounter (_ + 1)
              , children: [ R.text $ "Increment: " <> show counter ]
              }
          ]

main :: Effect Unit
main = do
  container <- getElementById "container" =<< (map toNonElementParentNode $ document =<< window)
  case container of
    Nothing -> throw "Container element not found."
    Just c -> do
      ex <- mkExample
      render (ex unit) c

Hitting the increment button runs the useEffect hook but does not run the useEffectAlways hook. I expected the -Always hook to run at least as often as any sibling effect hook, but it appears this is not the case. Could you help me understand why?

Thanks for an awesome library 🙏 !

@maddie927
Copy link
Member

Oh interesting.. I'll have to look into it. Thanks for reporting!

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

Successfully merging a pull request may close this issue.

2 participants