score:0

if you want to get your state value in the reducer you don't need getstate(). the state value is the first argument in your reducer. so you just call the state in your reducer. if you want to get the state value in your action then you can use getstate. also if you want to extract blogs object from the state object you are going one too far.

your are doing this:

const { blogs } = store.getstate().blogs;

and it should be this:

const { blogs } = store.getstate();

here is an example.

reducer:

const initialstate = {
  //your initial state
}

export default (state = initialstate, action) => {
  switch(action.type){
    case "update_user":
      // just use the state object
      const { blogs } = state;
      const username = action.data.id.username;
      const usertochange = blogs.find(a => a.username === username);
      const blogsneedtochange = usertochange.blogs
      const changeduserblogs = ?????????????
      return state.map(user =>
        user.username !== username ? user : changeduserblogs
      );
  }
}

action:

export const updateuser = (id, blog) => {
  return async (dispatch, getstate) => {
    const { blogs } = getstate();
    dispatch({
      type: "update_user",
      data: {
        id: id,
        data: blog,
      }
    });
  };
};

score:1

there is a faq part in the redux documentation about this issue: https://redux.js.org/faq/reducers#how-do-i-share-state-between-two-reducers-do-i-have-to-use-combinereducers

you could use reduce-reducers for example:

// in this reducer, the state argument represents your root state
// here you can handle actions which need to access multiple slice of the root state
const specializedreducer = (state, action) => {
  switch (action.type) {
    case "update_user":
      const { blogs, users } = state;
      // proceed with your code here
  }
}

const reducer = combinereducers({
  blogs: blogreducer,
  notification: notificationreducer,
  search: searchreducer,
  filter: filterreducer,
  users: userreducer,
  loggeduser: loginreducer
});

const rootreducer = reducereducers(specializedreducer, reducer);

const store = createstore(rootreducer, composewithdevtools(applymiddleware(thunk)));

score:1

so, as suspected, i was overly complicating things. thanks to the back and forth here, i realized that i could call another listener from another reducer. thus this is how i solve my issue.

in my user reducer i added a case "user_posted_blog"

 case "user_posted_blog":
      return [...action.data];

and then in my blog reducer i added an additional dispatch to that newly created case, which would grab all of the users and update the state after the new blog is created

export const createblog = content => {
  return async dispatch => {
    const newblog = await blogservice.create(content);
    dispatch({
      type: "new_blog",
      data: newblog
    });
    const updatedusers = await userservice.getall();
    dispatch({
      type: "user_posted_blog",
      data: updatedusers
    });
  };
};

Related Query

More Query from same tag