Shared state between all users
See original GitHub issueHello,
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:
- Created 2 years ago
- Reactions:1
- Comments:33 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It has same symptoms. Also we cannot reproduce it locally, only in production/preprod after server builds up some cache probably.
Our setup: