Прмогите переписать с if на switch
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 шт):
Автор решения: Faraday
→ Ссылка
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;
}
}
Вроде бы правильно, может что-то упустил, но суть вы поняли (Надеюсь)