Skip to content
This repository has been archived by the owner on May 30, 2020. It is now read-only.

Bug: Automatic dispatch of first action #3

Open
jasonzoladz opened this issue Mar 10, 2016 · 1 comment
Open

Bug: Automatic dispatch of first action #3

jasonzoladz opened this issue Mar 10, 2016 · 1 comment
Labels

Comments

@jasonzoladz
Copy link

A minimal-ish reproduction of the issue is set out below. I'm using purescript-redux with purescript-react. In short, the first reducer action (DECREMENT) is automatically dispatched, despite that no dispatch is made. In the example below, the initial state is 0, but it's immediately decremented.

module Main where

import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Console

import Data.Maybe.Unsafe (fromJust)
import Data.Nullable (toMaybe)

import DOM (DOM())
import DOM.HTML (window)
import DOM.HTML.Types (htmlDocumentToDocument)
import DOM.HTML.Window (document)

import DOM.Node.NonElementParentNode (getElementById)
import DOM.Node.Types (Element(), ElementId(..), documentToNonElementParentNode)

import React
import ReactDOM (render)
import React.DOM as D
import React.DOM.Props as P

import Control.Monad.Eff.Redux

type AppState = Int

incrementA = "INCREMENT"
decrementA = "DECREMENT"

mainReducer :: forall a b. AppState -> Action a b -> AppState
mainReducer state action =
  case action.type of
    decrementA -> state - 1
    incrementA -> state + 1
    _          -> state

home :: forall props. ReactClass Store
home = createClass $ spec unit $ \ctx -> do
  store <- getProps ctx
  state <- getState store
  log $ ("The state is: " ++ (show (state :: Int)))
  return $
    D.h1 []
         [ D.text "The state should be 0, but instead it's:  "
         , D.text (show (state :: Int))
         ]

main :: forall eff.  Eff (dom :: DOM, reduxM :: ReduxM | eff ) Unit
main = do
  store <- createStore mainReducer (0)
  let appRender = (elm' >>= render (ui store) >>= \_ ->  return unit)
  subscribe appRender store
  appRender
  where
    ui :: Store -> ReactElement
    ui str = D.div' [ createFactory home str]


elm' :: forall eff. Eff (dom :: DOM | eff) Element
elm' = do
  win <- window
  doc <- document win
  elm <- getElementById (ElementId "app") (documentToNonElementParentNode (htmlDocumentToDocument doc))
  return $ fromJust (toMaybe elm)
@brakmic
Copy link
Owner

brakmic commented Mar 10, 2016

Hi @jasonzoladz,

Thanks for this bug-report. I must admit that I'll need some time to understand it as I have no experience with purescript-react.

@brakmic brakmic added the bug label Mar 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants