简体   繁体   English

更新 Redux 中的存储以获取参考字段

[英]Updating Store in Redux for Reference Field

I have a blog application, with a Blog and a User model, both with references to one another.我有一个博客应用程序,其中有一个博客和一个用户 model,两者都相互引用。 A User has a one to many relationship with Blog model.用户与博客 model 具有一对多关系。 I need to display a full list of blogs on a Blogs page and a full list of users on a Users page.我需要在博客页面上显示完整的博客列表,在用户页面上显示完整的用户列表。

If a logged user creates a blog post, my action creator from my Blogs reducer saves the blog to the database, and updates the BLOGS state.如果登录的用户创建了博客文章,我的 Blogs reducer 中的操作创建者会将博客保存到数据库,并更新 BLOGS state。 My post call to the back-end is saving the blog post AND the user data to the database but i don't have any actions in my User reducer file to update the user state.我对后端的帖子调用正在将博客帖子和用户数据保存到数据库中,但我的 User reducer 文件中没有任何操作来更新用户 state。

On my app's initial render, i load all of the users and the blogs.在我的应用程序的初始渲染中,我加载了所有用户和博客。

Thus my inital store's state upon user login looks like this:因此,用户登录时我的初始商店的 state 如下所示:

LOGGED_USER
BLOGS
USERS

I am trying to update the state of the User associated with the new Blog post, but having difficulty in figuring out how to do so.我正在尝试更新与新博客文章关联的用户的 state,但很难弄清楚如何执行此操作。 I was thinking that I might have to reach into the shared store for the Blogs state in the update User action creator.我在想我可能必须进入更新用户操作创建器中的博客 state 的共享商店。

EDIT/UPDATE i realized that if i try to call an action creator to update the state of the blog, and then another separate one to call to update the state of the user, with the new blog, i start running into the potential of mismatched id's, since when a new blog post it is auto generating an ID.编辑/更新我意识到,如果我尝试调用操作创建者来更新博客的 state,然后再调用另一个单独的操作创建者来更新用户的 state,则使用新博客,我开始遇到不匹配的可能性id,因为当一个新的博客文章它会自动生成一个 ID。 Thus i think (but could very well be wrong) that i do need to make a call to the store to pickup the updated blog state when trying to update the user.因此,我认为(但很可能是错误的)在尝试更新用户时,我确实需要致电商店以获取更新的博客 state。

SO, it would go like this:所以,它会像这样 go :

CREATE BLOG POST FORM ON FRONT END with NEW BLOG CONTENT with addBlog handler在前端使用带有 addBlog 处理程序的新博客内容创建博客帖子表单

const addBlog = async event => {
    event.preventDefault();

    const blogObject = {
      url: newURL.value,
      title: newTitle.value,
      author: newAuthor.value
    };

    await props.createBlog(blogObject);
    await props.updateUser(props.loggedUser, blogObject);
    props.history.push("/blogs");
    setNotification(`New Blog Created!`, 4);
  };

CALL createBlog and updateUser action creators from my blogReducer and User Reducer files:从我的 blogReducer 和 User Reducer 文件中调用 createBlog 和 updateUser 操作创建者:

blogReducer.js blogReducer.js

const blogReducer = (state = [], action) => {
  switch (action.type) {
    case "NEW_BLOG":
      return [...state, action.data];

export const createBlog = content => {
  return async dispatch => {
    const newBlog = await blogService.create(content);
    dispatch({
      type: "NEW_BLOG",
      data: newBlog
    });
  };
};

userReducer.js userReducer.js

 **export const updateUser = (id, blog) => {
      return async dispatch => {
        dispatch({
          type: "UPDATE_USER",
          data: {
            id: id,
            data: blog
          }
        });
      };
    };**

const userReducer = (state = [], action) => {
  switch (action.type) {
    case "NEW_USER":
      return [...state, action.data];
    case "UPDATE_USER":
      const {blogs} = store.getState().blogReducer.blogs
      const username = action.data.id.username;
      console.log("Action Data", action.data);
      const userToChangeBlogs = blogs.find(a => a.user.username === username);
      console.log("User to Change", userToChange);
      const changedUser = userToChangeBlogs.blogs.concat(action.data.data);
      console.log("Change User", changedUser);
      return state.map(user =>
        user.username !== username ? user : changedUser
      );
    case "DELETE_USER":
      const removeObject = action.data;
      return state.filter(user => user.id !== removeObject.id);
    case "INIT_USERS":
      return action.data;
    default:
      return state;
  }
};

yet as i start to work on this updateUser action creator, i'm stumbling as to how to handle this.然而,当我开始研究这个 updateUser 动作创建者时,我对如何处理这个问题感到困惑。 ok, so i get the blogs state from the store, grab the username from the person who sent the new blog form and the new blog content.好的,所以我从商店获取博客 state,从发送新博客表单和新博客内容的人那里获取用户名。 Then i'm looking in the blogs store state for that user from the submitted form然后我从提交的表单中查看该用户的博客商店 state

....and then i DO WHAT...i've reached a dead end? ....然后我做什么...我走到了死胡同? Do i just reach into that users state and replace with all of the updated blogs from the blogs store?我是否只是接触到该用户 state 并替换为博客商店中所有更新的博客?

I feel LIKE i am making this way more complicated than it needs to be.我觉得我让这种方式变得比它需要的更复杂。

Your action.data object has the data key you want itself, as well as the id key.你的action.data object 有你自己想要的data键,以及id键。

You should probably modify: const changedUser = userToChange.blogs.concat(action.data) by const changedUser = userToChange.blogs.concat(action.data.data)您可能应该修改: const changedUser = userToChange.blogs.concat(action.data) by const changedUser = userToChange.blogs.concat(action.data.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM