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

Help needed: dipatch, getState not being made available to action creators #99

Open
mnazim opened this issue Nov 2, 2016 · 0 comments

Comments

@mnazim
Copy link

mnazim commented Nov 2, 2016

dipatch and getState not being made available to async action creators during server side rendering. Everything seems to work fine on client side. I am including the relevant parts of the code below. I can't seem to put my finger on what I am missing here. Thanks in advance for any help/pointers.

I am using Express.js middleware to render server side like so,

app.use((req, res, next) => {
  const memoryHistory = createHistory(req.originalUrl)
  const preloadedState = {}
  const store = configureStore()
  const history = syncHistoryWithStore(memoryHistory, store)

  match({ history, routes, location: req.originalUrl }, (error, redirectLocation, renderProps) => {
    if (redirectLocation) {
      res.redirect(redirectLocation.pathname + redirectLocation.searc)
    } else if (error) {
      console.error('ROUTER ERROR :', error)
      res.status(500)
    } else if (renderProps){
      loadOnServer(renderProps, store).then(() =>{
        console.info(store)
        const component = (
          <Provider store={store} key="provider">
            <ReduxAsyncConnect {...renderProps} />
          </Provider>
        )
        res.status(200)
        global.navigator = {userAgent: req.headers['user-agent']}

        res.send('<!doctype html>\n' +
                 ReactDOM.renderToString(
                 <HTML
                   component={component}
                   store={store} />)
        )
      })
    } else {
      res.status(404).send('Not Found')
    }
  })

})

I have added reduxAsyncConnect reducer like so

import { reducer as reduxAsyncConnect } from 'redux-async-connect';

const rootReducer = combineReducers({
  // other reducers removed
  reduxAsyncConnect
})

Here's what my routes look like,

const App = ({ store, history }) => (
  <Provider store={store} key="provider">
      <Router history={history}
    render={(props) => <ReduxAsyncConnect {...props} filter={item => !item.deferred} />}>
          {routes}
      </Router>
  </Provider>
)


const store = configureStore()
const history = syncHistoryWithStore(browserHistory, store)

render(
  <App store={store} history={history} />,  document.getElementById('root')
)

Here's the root component

@asyncConnect([{
  promise: () => {
    const promises = [];
    promises.push(loadFlatPages());
    return Promise.all(promises);
  }
}])
@connect(
  state => (state),
  { push,
    loadFlatPages,
  }
)
class Root extends Component {
  constructor(props) {
    super(props)
  }

  componentWillMount() {
    this.props.loadFlatPages()
  }

  render() {

    return (
      <div> {* markup snipped *} </div>
    )
  }
}

export default Root

Here are my action creators. Here I get error UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'dispatch' of undefined

function fetchFlatPages() {
  return {
    [CALL_API]: {
      types: makeActionTriplet(actionTypes.FETCH_FLATPAGES),
      endpoint: 'pages/',
      method: 'get',
    }
  };
}

export function loadFlatPages() {
  return (dispatch, getState) => {
    return dispatch(fetchFlatPages());
  };
}
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

1 participant