离线第一:重构链接请求在使用效果中混乱
我必须开发一个应用程序,该应用程序的先决条件是将本地数据(indexedDB)作为优先级(无论网络是否可用。目标是不超载网络)。这是我的第一个ReactJS应用程序,我知道我没有正确的反射,
我设法编码一些东西,但这是一团糟,可能是货车。我必须在几页上应用相同的原则 我编码的页面非常简单:它显示供应商列表。此列表来自API,但我想将其存储在indexedDB中,然后使用IndexedDB中的数据。供应商清单不会改变
有人可以指导我写一些清洁的东西吗?
const SUPPLIERS_URL = "/suppliers.php";
const Fournisseurs = () => {
const [suppliers, setSuppliers] = useState([]);
const [nbSuppliers, setNbSupplier] = useState();
useEffect(() => {
const fetchFournisseurs = () => {
const dbOpenRequest = indexedDB.open("salon", 1);
// https://stackoverflow.com/questions/33054891/transactioninactiveerror-failed-to-execute-add-on-idbobjectstore-the-trans
dbOpenRequest.onsuccess = function (event) {
const db = event.target.result;
const txn = db.transaction("suppliers", "readwrite");
const store = txn.objectStore("suppliers");
const countSuppliers = store.count();
countSuppliers.onsuccess = function () {
setNbSupplier(countSuppliers.result);
console.log("nb fou", countSuppliers.result);
};
if (nbSuppliers !== 0) {
console.log("des suppliers dans indexeddb");
const getAllRequest = store.getAll();
getAllRequest.onsuccess = function () {
setSuppliers(getAllRequest.result);
};
}
};
};
fetchFournisseurs();
const suppliersFromApi = async () => {
console.log(" suppliersFromApi pas de suppliers dans indexeddb");
await axios.post(SUPPLIERS_URL, "data").then(function (response) {
console.log("list fou", suppliers);
const dbOpenRequest = indexedDB.open("salon", 1);
dbOpenRequest.onsuccess = function (event) {
const db = event.target.result;
const txn = db.transaction("suppliers", "readwrite");
const store = txn.objectStore("suppliers");
suppliers.forEach((supplier) => {
console.log("ajout fou");
store.add(supplier);
});
};
setSuppliers(response.data);
setNbSupplier(response.data.count());
console.log("nb de fou ds idb apr insert", response.data.count(), nbSuppliers);
});
console.log(suppliers);
};
if (nbSuppliers === 0) {
suppliersFromApi();
}
}, [nbSuppliers]);
I have to develop an application which has the prerequisite of using local data (indexeddb) as a priority (whether the network is available or not. The goal is not to overload the network). It's my first reactjs app and I know I don't have the right reflexes
I managed to code something but it's a mess and probably buggy. I'll have to apply the same principle on several pages
The page I coded is very simple: it displays a list of suppliers. This list comes from an api but I want to store it in indexeddb and use the data that is in indexeddb then. The list of suppliers won't change
Can someone guide me to write something cleaner ?
const SUPPLIERS_URL = "/suppliers.php";
const Fournisseurs = () => {
const [suppliers, setSuppliers] = useState([]);
const [nbSuppliers, setNbSupplier] = useState();
useEffect(() => {
const fetchFournisseurs = () => {
const dbOpenRequest = indexedDB.open("salon", 1);
// https://stackoverflow.com/questions/33054891/transactioninactiveerror-failed-to-execute-add-on-idbobjectstore-the-trans
dbOpenRequest.onsuccess = function (event) {
const db = event.target.result;
const txn = db.transaction("suppliers", "readwrite");
const store = txn.objectStore("suppliers");
const countSuppliers = store.count();
countSuppliers.onsuccess = function () {
setNbSupplier(countSuppliers.result);
console.log("nb fou", countSuppliers.result);
};
if (nbSuppliers !== 0) {
console.log("des suppliers dans indexeddb");
const getAllRequest = store.getAll();
getAllRequest.onsuccess = function () {
setSuppliers(getAllRequest.result);
};
}
};
};
fetchFournisseurs();
const suppliersFromApi = async () => {
console.log(" suppliersFromApi pas de suppliers dans indexeddb");
await axios.post(SUPPLIERS_URL, "data").then(function (response) {
console.log("list fou", suppliers);
const dbOpenRequest = indexedDB.open("salon", 1);
dbOpenRequest.onsuccess = function (event) {
const db = event.target.result;
const txn = db.transaction("suppliers", "readwrite");
const store = txn.objectStore("suppliers");
suppliers.forEach((supplier) => {
console.log("ajout fou");
store.add(supplier);
});
};
setSuppliers(response.data);
setNbSupplier(response.data.count());
console.log("nb de fou ds idb apr insert", response.data.count(), nbSuppliers);
});
console.log(suppliers);
};
if (nbSuppliers === 0) {
suppliersFromApi();
}
}, [nbSuppliers]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论