purescript-redux
purescript-redux copied to clipboard
Bug: Automatic dispatch of first action
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)
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.