简体   繁体   English

从 state 中删除 object 后,组件未重新加载,我正在使用 React-redux

[英]Component isn't reloading after deleting an object from the state, I'm using React-redux

I'm working on an online streaming app where the user can create, edit, delete, and host streams.我正在开发一个在线流媒体应用程序,用户可以在其中创建、编辑、删除和托管流媒体。 The problem is when I try deleting the stream, it deletes it in the database but my main component where the streams are being displayed doesn't reload, although it reloads itself when the user creates or edits the stream.问题是当我尝试删除 stream 时,它会在数据库中将其删除,但显示流的主要组件不会重新加载,尽管它会在用户创建或编辑 stream 时自行重新加载。

Here's my action creator:这是我的动作创建者:

export const deleteStream = (id) => async dispatch => {
  await streams.delete(`/streams/${id}`);

  dispatch({ type:'DELETE_STREAMS', payload: id });
  history.push('/');
}

And here's the reducer:这是减速器:

const streamReducer = (state = {}, action) => {
  switch(action.type){
    case 'DELETE_STREAM':
      return {...state, [action.payload]: undefined};
      //I also used lodash to delete it alternatively as- return _.omit(state, action.payload);
    default:
      return state;
  }
}

Also, not to forget that the objects are key interpolated in my server ie, instead of having an array of objects, I have an object of objects.另外,不要忘记对象是在我的服务器中插入的键,即,我有一个 object 对象,而不是对象数组。

Plural problem!复数问题! DELETE_STREAMS vs DELETE_STREAM . DELETE_STREAMSDELETE_STREAM You dispatch the former and reduce on the latter.您派遣前者并减少后者。

This is why it's always a good idea to have your actions defined somewhere even of its just export const X = "X" .这就是为什么在某个地方定义你的操作总是一个好主意,即使它只是export const X = "X" Then always reference them instead of using string literals.然后总是引用它们而不是使用字符串文字。

It wasn't an error updating your React rendering, but that redux was never updated.更新你的 React 渲染不是错误,但 redux 从未更新。 A great tool to debug this is redux devtools https://github.com/reduxjs/redux-devtools , you can see the state of redux and every dispatched action and it's impact. A great tool to debug this is redux devtools https://github.com/reduxjs/redux-devtools , you can see the state of redux and every dispatched action and it's impact.

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

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