Skip to content
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

Action never seen when dispatched on Store. #1736

Closed
ramakay opened this issue May 16, 2016 · 3 comments
Closed

Action never seen when dispatched on Store. #1736

ramakay opened this issue May 16, 2016 · 3 comments

Comments

@ramakay
Copy link

ramakay commented May 16, 2016

I have a non React project with the following code
.

index.js

import configureStore from 'js/store/configureStore'

/* Redux Imports */
import { createPage } from 'js/actions/page'

// Boot the App
document.addEventListener('DOMContentLoaded', function () {
  const initialState = window.__INITIAL_STATE__
  const pageId = window.__INITIAL_STATE__.page
  const store = configureStore(initialState)
  store.dispatch(createPage(pageId))
})

*actions/page.js*
function initPage (pageId) {
  return {
    type: 'CREATE_PAGE',
    page: pageId
  }
}

export function createPage (pageId) {
  return dispatch => {
    dispatch(initPage(pageId))
  }
}

reducers/page.js

/**
 * Page Mediators get imported here
 * Page Mediators are brokers that hold page initialization.
 * The bundle includes all page mediators but they will not be activated
 * unless thre reducer calls them.
 */
import * as cart from 'js/templates/cart'

/**
 * This is a edge case, index.js boots the app and we are subscribing to 
 * a Redux @@INIT action instead of adding one more layer of actions
 * which leads to unintended scenarios.
 */

const page = (state = {page: []} , action) => {

  console.log(state)
  switch (action.type) {
    case '@@INIT':
      console.log('initializing page:' + state.id)
      console.log(state)
      dispatch({type: 'CREATE_PAGE',page: state.id})
      cart.init()
      return state
    case 'CREATE_PAGE':
      console.log('initializing page:' + state.id)
      cart.init()
      return state
    default:
      return state
  }
}
export default page

The problem I have is that actions/page.js never sees the CREATE_PAGE action. Any thoughs?

@gaearon
Copy link
Contributor

gaearon commented May 16, 2016

Unrelated but

  • This is a edge case, index.js boots the app and we are subscribing to
  • a Redux @@init action instead of adding one more layer of actions
  • which leads to unintended scenarios.

can break your app any day.

We specifically say:

  • These are private action types reserved by Redux.
  • For any unknown actions, you must return the current state.
  • If the current state is undefined, you must return the initial state.
  • Do not reference these action types directly in your code.

@gaearon gaearon mentioned this issue May 16, 2016
8 tasks
@ramakay
Copy link
Author

ramakay commented May 16, 2016

Thanks @gaearon - I intend to remove this, I added it because the actions I have created are never seen - What could I be doing wrong?

@gaearon
Copy link
Contributor

gaearon commented May 16, 2016

Please use StackOverflow for questions. We try to use the issue tracker for bug and feature discussions, but not for support.

As for the code, it violates the constraints of Redux by performing a side effect (cart.init()) and trying to dispatch an action (dispatch()) inside the reducer. This is not allowed, as noted more than once in the documentation. Reducers must be pure functions, so you should place side effects before or after dispatching the action.

If you dispatch inside a reducer, your code should fail. If it doesn’t fail, maybe you have a Promise swallowing the error somewhere. It’s hard to say more from this snippet of code.

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

2 participants