question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it Possible to Access ctx in makeStore?

See original GitHub issue

I read the source code and saw that ctx is unpacked into the options.

Screen Shot 2019-07-31 at 10 43 24

Sadly it seems like it is empty. Console logging the options gives me

Screen Shot 2019-07-31 at 10 46 50

I use next-redux-wrapper like this:

class MyApp extends App {
  /* ... */
}
export default withRedux(makeStore, { debug: true })(MyApp);

Am I doing something wrong? For now I had to write my own Redux wrapper because I want to use redux-cookies-middleware and the getCookie/setCookie functions need ctx to work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:30 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
kirill-konshincommented, Feb 21, 2020

Req has cookies in headers. You may add cookie-parser middleware.

1reaction
adonigcommented, Feb 21, 2020

@oyeanuj The only “right” way to do it I found was to do it in makeStore like this:

export default (initialState = { router: undefined }, options) => {
  const { req, res, asPath } = options;
  const ctx = { req, res };
  if (asPath) {
    initialState.router = initialRouterState(asPath);
  }

  const store = createStore(
    reducer,
    getStateFromCookies(initialState, paths, name => {
      const cookie = getCookies(ctx, name);
      return (
        cookie || initialState[name] // NOTE: If the cookie is undefined, DO NOT overwrite the initialState with it!
      );
    }),
    bindMiddleware([
      createRouterMiddleware(),
      reduxCookiesMiddleware(paths, {
        setCookie: (name, value) => {
          setCookies(ctx, name, value);
        }
      }),
      thunkMiddleware
    ])
  );

  return store;
};

Note the comment, it is important or you’ll introduce a hard to find bug. It’s probably even better not to do it at all, see here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ERROR TypeError: makeStore is not a function - Stack Overflow
Not sure what the withRedux api is but based on the error it seems it's expecting a function but you are passing an...
Read more >
Next.js — Setting up a Redux store with combineReducers()
Your Next server takes the currently store state after running getInitialProps() and call the makeStore() again to create a new store for components...
Read more >
Next-redux-wrapper NPM
Although it is possible to create server or client specific logic in both createStore function and getInitialProps method I highly don't recommend to...
Read more >
Server-Side Rendered App with Next.js, React and Redux
To get started with next , we need to edit our package.json in ... When using it on normal pages, getInitialProps only has...
Read more >
Next Js React Redux Authentication Tutorial - Kaloraat
This way pages will be able to access redux store as props. ... While inside _app you can destructure Component, router and ctx(context)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found