Помогите переписать с if на switch

Рейтинг: -4Ответов: 1Опубликовано: 14.01.2023
dispatch(action) {
    if (action.type === "ADD-POST") {
      let newPost = {
        id: 5,
        message: this._state.profilePage.newPostText,
        likeCount: 0,
      };
      this._state.profilePage.postData.push(newPost);
      this._rerenderEntireTree(this._state);
    } else if (action.type === "UPDATE-NEW-POST-TEXT") {
      this._state.profilePage.newPostText = action.newText;
      this._rerenderEntireTree(this._state);
    } else if (action.type === "REMOVE-POST") {
      let newPost = {
        id: 5,
        message: this._state.profilePage.newPostText,
        likeCount: 0,
      };
      this._state.profilePage.postData.pop(newPost);
      this._rerenderEntireTree(this._state);
    }
},

Ответы

▲ -1
switch (action.type)
{
   case "ADD-POST":
   {
      let newPost = {
         id: 5,
         message: this._state.profilePage.newPostText,
         likeCount: 0,
      }
      this._state.profilePage.postData.push(newPost);
      this._rerenderEntireTree(this._state);

      break;
   }
   case "UPDATE-NEW-POST-TEXT":
   {
      this._state.profilePage.newPostText = action.newText;
      this._rerenderEntireTree(this._state);

      break;
   }
   case "REMOVE-POST":
   {
      let newPost = {
         id: 5,
         message: this._state.profilePage.newPostText,
         likeCount: 0,
      };
      this._state.profilePage.postData.pop(newPost);
      this._rerenderEntireTree(this._state);

      break;
   }

}

Вроде бы правильно, может что-то упустил, но суть вы поняли (Надеюсь)