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.

Shared state between all users

See original GitHub issue

Hello,

The state of our application seems to be shared between users during hydratation from the server.

Example scenario

User A logs in and fetches his datas User B get the User A datas in his store from the HYDRATE action

Code

Here is our store.js

import { createStore, applyMiddleware } from "redux";
import { createWrapper } from "next-redux-wrapper";
import reducers from "../reducers";
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';

const composeEnhancers = composeWithDevTools({});

const makeStore = () => createStore(reducers, composeEnhancers(applyMiddleware(thunk)));

export const wrapper = createWrapper(makeStore, { debug: false });

reducers.js

import { combineReducers } from 'redux';

import ErrorReducer from './error_reducer';
import ApplicationReducer from './application_reducer';
import { HYDRATE } from "next-redux-wrapper";

const combinedReducers = combineReducers({
  error: ErrorReducer,
  application: ApplicationReducer,
});

const allReducers = (state, action) => {
  if (action.type === HYDRATE) {
    const nextState = {
      ...state,
      ...action.payload,
    };
    if (state.count) nextState.count = state.count;
    return nextState;
  }
  else {
    return combinedReducers(state, action);
  }
}

export default allReducers;

_app.js

import React from "react";
import App from "next/app";
import { wrapper } from "../utils/store";

class WrappedApp extends App {
  render() {
    const { Component, pageProps } = this.props;

    return (
      <>
        <Component {...pageProps} />
      </>
    );
  }
}

export default wrapper.withRedux(WrappedApp);

Page example

import React, { PureComponent } from 'react';
import { connect } from "react-redux";

class Home extends PureComponent {

    static async getInitialProps() {
       ...
    }

    render() {
        ...
    }
}

const mapStateToProps = (state) => {
    return {
        ...
    };
}

const mapDispatchToProps = (dispatch) => {
    return {
        ...
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(Home);

We got this bug since we updated all our modules: next: 4.2.1 -> 10.0.9 next-redux-wrapper: 1.3.4 -> 6.0.2 react-redux: 5.0.6 -> 7.2.3 redux: 3.5.2 -> 4.0.5

Has anyone encoutered this problem before or have any idea what cause it ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:33 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
kirill-konshincommented, Aug 27, 2021

@kirill-konshin thanks for the quick answer!

Sorry to bother but how do I create a new instance of the store every time when it’s called?

const makeStore = () => configureStore<OurStore>({
  reducer: rootReducer,
});
2reactions
NikStepMnstrcommented, Jul 29, 2021

It has same symptoms. Also we cannot reproduce it locally, only in production/preprod after server builds up some cache probably.

Our setup:

  • next: 11.0.1,
  • next-redux-wrapper: 7.0.0-rc.2,
  • redux: 4.0.5,
  • react-redux: 7.2.3,
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to share state between users in ReactJS - Stack Overflow
I'm building a multiplayer card game mainly in ReactJS and want to share some state between users (players). I need each user to...
Read more >
How To Share State Across React Components with Context
In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information ...
Read more >
How to share state between multiple components without ...
How to share state between multiple components without passing them in props (React, Context API). This article will deal with a question you ......
Read more >
How to share state across React Components with context
In a small app, React Context along with useState is the best way to share state or simply pass data around components without...
Read more >
Introduction to State - React - LearnHowToProgram.com
An example of shared state is the main list of all tickets in the Help Queue. Our ticket list component will need access...
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