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

redux state will become to the initial State after page load finished. #51

Closed
joyran opened this issue Sep 6, 2017 · 4 comments
Closed

Comments

@joyran
Copy link

joyran commented Sep 6, 2017

thanks for your awesome library.

I have a problem, when refresh the browser, getInitialProps fetch data and dispatch an action ,then the server will return a correct page, but when the client loads all js file, redux state has become the initial value.

demo repository https://github.com/joyran/next.js-with-redux-demo

// pages/index.js
// init state
const initialState = {
  name: null,
  bio: null
};

const initStore = () => {
  return createStore(user, initialState, composeWithDevTools(applyMiddleware(thunkMiddleware)));
};

const Index = () => {
  return (
    <User />
  );
};

Index.getInitialProps = async ({ store, isServer }) => {
  const res = await fetch('https://api.github.com/users/tj');
  const data = await res.json();

  store.dispatch(readUserSuccessByServer(data));
};

export default withRedux(initStore, null)(Index);
// reducers
import fetch from 'isomorphic-fetch';
import { createActions, handleActions } from 'redux-actions';

// ------------------------
// ACTIONS
// ------------------------
export const {
  readUserSuccess,
  readUserSuccessByServer
} = createActions(
  'READ_USER_SUCCESS',
  'READ_USER_SUCCESS_BY_SERVER'
);

export const readUser = () => dispatch => {
  fetch('https://api.github.com/users/a')
    .then(res => res.json())
    .then(res => dispatch(readUserSuccess(res)))
    .catch((err) => {
      console.error(err.message);
    });
};

// ------------------------
// REDUCERS
// ------------------------
export const user = handleActions({
  READ_USER_SUCCESS: (state, action) => ({
    ...state,
    name: action.payload.name,
    bio: action.payload.bio
  }),

  READ_USER_SUCCESS_BY_SERVER: (state, action) => ({
    ...state,
    name: action.payload.name,
    bio: action.payload.bio
  })
}, {});

console log

warning.js?9dca99b:35 Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
 (client) h1 data-reactid="3"></h1><h3 data-reacti
 (server) h1 data-reactid="3">TJ Holowaychuk</h1><
@kirill-konshin
Copy link
Owner

You have a bug in your code:

const initStore = () => {
  return createStore(user, initialState, composeWithDevTools(applyMiddleware(thunkMiddleware)));
};

initialState has to be used from function params:

const initStore = (initialState) => {
  return createStore(user, initialState, composeWithDevTools(applyMiddleware(thunkMiddleware)));
};

Otherwise same state will always be applied and server-propagated state will be lost.

@joyran
Copy link
Author

joyran commented Sep 7, 2017

thank you very much

@steveinatorx
Copy link

@kirill-konshin this statement "initialState has to be used from function params - Otherwise same state will always be applied and server-propagated state will be lost."
was the key to fixing my problem with page child components resetting the state. Might be useful to others to add that as a comment in your readme example...

also thanks this is a very useful lib !

@kirill-konshin
Copy link
Owner

Pr is welcome )

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

3 participants