【发布时间】:2020-09-09 23:03:49
【问题描述】:
我正在通过 redux-saga 实现删除产品功能,这是我的 deleteProduct 功能代码
import firebaseApp from "./config";
const firebaseDb = firebaseApp.database();
export const deleteProduct = (productId) => {
return firebaseDb
.ref("products")
.child(productId)
.remove()
.then(() => {
console.log(`Deleted ${productId}`)
return { status: "ok" }
})
.catch(() => ({ status: "error" }));
}
在我运行click to delete按钮后,它已经显示产品id已被删除,但产品数据并没有从数据库中删除。
这是我的reducer
const deleteProductRequest = (state, action) => ({
...state,
loading: true,
type: action.type,
});
const deleteProductSuccess = (state, action) => ({
...state,
loading: false,
product: action.data,
type: action.type,
});
const deleteProductFailure = (state, action) => ({
...state,
loading: false,
error: action.error,
type: action.type,
});
这是我的saga
export function* deleteProductRequest(action) {
try {
const { productId } = action;
const response = yield call(deleteProduct, productId);
if ((response.status === "ok")) {
yield put(Creators.deleteProductSuccess(response.product));
} else {
yield put(Creators.deleteProductFailure(response.error));
}
} catch (error) {
yield put(Creators.deleteProductFailure(error));
}
}
我的容器是这样的
import { connect } from "react-redux";
import { Creators } from "../../actions/productAction";
import ProductTable from "../../pages/ProductTable";
const mapStateToProps = (state) => ({
products: state.product.products,
product: state.product.product,
loading: state.product.loading,
error: state.product.error,
});
const mapDispatchToProps = {
...Creators,
};
const PostNewContainer = connect(
mapStateToProps,
mapDispatchToProps
)(ProductTable);
export default PostNewContainer;
首先,我将所有产品列表从 Firebase 获取到一个表中,然后我开始执行产品操作,并逐步实现 API、reducer 和 Saga
任何人都了解此案,请您支持我。非常感谢
【问题讨论】:
标签: reactjs firebase firebase-realtime-database redux-saga