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

Unexpected key "offline" found in previous state received by the reducer. Expected to find one of the known reducer keys instead: "posts", "comments", "routing". Unexpected keys will be ignored. #27

Closed
TheoMer opened this issue Mar 30, 2017 · 1 comment

Comments

@TheoMer
Copy link

TheoMer commented Mar 30, 2017

So, there appears to be am issue with combineReducers (See attached error image). My code is as follows:

index.js

import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux'; // we need this for react-router
import posts from './posts';
import comments from './comments';

const rootReducer = combineReducers({ posts, comments, routing: routerReducer });

export default rootReducer;

posts.js

function posts(state = [], action) {
  switch (action.type) {
    case 'FETCH_POSTS':
      return action.payload;
    case 'INCREMENT_LIKES' :
      const i = action.index;
      return state;
    default:
      return state;
  }
}

export default posts;

comments.js

function comments(state = {}, action) {
  if (action.type === 'FETCH_COMMENTS') {
    return action.payload;
  } else if (action.type === 'ADD_COMMENT') {
    return state;
  } else if (action.type === 'REMOVE_COMMENT') {
    return state;
  };
  return state;
}

export default comments;

actionCreators.js

// fetch posts
export function fetchPosts() {
  console.log("Load Posts has been fired");
  return dispatch => {
    Posts.on('value', posts => {
      dispatch({
        type: 'FETCH_POSTS',
        payload: posts.val(),
        meta: {
          offline: {
            effect: {},
            commit: { type: 'FETCH_POSTS_COMMIT', meta: { posts } },
            rollback: { type: 'FETCH_POSTS_ROLLBACK', meta: { posts } },
          }
        }
      });
    });
  };
}

// fetch comments
export function fetchComments() {
  console.log("Load Comments has been fired");
  return dispatch => {
    Comments.on('value', comments => {
      dispatch({
        type: 'FETCH_COMMENTS',
        payload: comments.val(),
        meta: {
          offline: {
            effect: {},
            commit: { type: 'FETCH_COMMENTS_COMMIT', meta: { comments } },
            rollback: { type: 'FETCH_COMMENT_ROLLBACK', meta: { comments } },
          }
        }
      });
    });
  };
}

export function increment(i) {
  let currentLikesVal = 0;

  // Retrieve the current value of likes
  Posts.child(i).child('likes').once("value", function(itemData) {
    currentLikesVal = itemData.val() + 1;
  });

  var updateData = {
    likes: currentLikesVal
  }

  return dispatch => {
    dispatch({
      type: 'INCREMENT_LIKES', index: 1,
      payload: Posts.child(i).update(updateData),
      meta: {
        offline: {
          effect: {},
          commit: { type: 'INCREMENT_LIKES_COMMIT', meta: { updateData } },
          rollback: { type: 'INCREMENT_LIKES_ROLLBACK', meta: { updateData } },
        }
      }
    });
  };
}

/*
  Comments
*/

export function addComment(postId, author, comment) {
  var postData = {
    text: comment,
    user: author
  };

  return dispatch => {
    dispatch({
      type: 'ADD_COMMENT', postId, author, comment,
      payload: Comments.child(postId).push(postData),
      meta: {
        offline: {
          effect: {},
          commit: { type: 'ADD_COMMENT_COMMIT', meta: { postData } },
          rollback: { type: 'ADD_COMMENT_ROLLBACK', meta: { postData } },
        }
      }
    });
  } 
}

export function removeComment(postId, i){
  console.log('i in removeCommet action is = ' + i)
  return dispatch => {
    dispatch({
      type: 'REMOVE_COMMENT', i, postId,
      payload: Comments.child(postId).child(i).remove(),
      meta: {
        offline: {
          effect: {},
          commit: { type: 'REMOVE_COMMENT_COMMIT', meta: { postId } },
          rollback: { type: 'REMOVE_COMMENT_ROLLBACK', meta: { postId } },
        }
      }
    });
  };
}

error

@jevakallio
Copy link
Collaborator

Hi! Thanks for submitting this. It's a duplicate of #19, so let's track this there.

I am planning fix this later today.

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

2 participants