如何解决MSW随机404误差与React
我已经开始使用MSW嘲笑BE,但是当我保存更改时,它随机地无法获取数据。不要真正知道如何调试,因为它是非常随机的
API路由功能
import Api from './Api';
export default class BrandsAPIs extends Api {
getAllBrands(accessToken) {
return this.instance.get('brands', this.constructor.addAuthorizationHeader(accessToken));
}
}
browser.js
import { setupWorker, rest } from 'msw';
import brands from './getBrands.json';
const worker = setupWorker(
rest.post('/authentication', (req, res, ctx) => res(ctx.json({
accessToken: 'Freebee',
user: 'Andrea',
}))),
rest.get('/brands', (req, res, ctx) => res(ctx.json(brands))),
);
worker.start();
react页面
import React, { useState, useEffect } from 'react';
import { BrandsAPIsSingleton } from '../../modules/api';
// components
import BrandsMainTable from './table/BrandsMainTable';
export default function Main() {
const [isLoading, setIsLoading] = useState([]);
const [brands, setBrands] = useState([]);
useEffect(() => {
setIsLoading(true);
BrandsAPIsSingleton.getAllBrands()
.then((r) => {
setBrands(r.data);
setIsLoading(false);
}).catch((error) => {
setIsLoading(false);
console.log(error);
});
}, []);
if (isLoading) {
return <div>Loading...</div>;
}
return (
<BrandsMainTable data={brands} />
);
}
其他人都有这个问题?
I have started using MSW to mock the BE, but randomly it fails to fetch data when I save changes. Dont' really know how to debug it, since it's very random
Api routing functions
import Api from './Api';
export default class BrandsAPIs extends Api {
getAllBrands(accessToken) {
return this.instance.get('brands', this.constructor.addAuthorizationHeader(accessToken));
}
}
browser.js
import { setupWorker, rest } from 'msw';
import brands from './getBrands.json';
const worker = setupWorker(
rest.post('/authentication', (req, res, ctx) => res(ctx.json({
accessToken: 'Freebee',
user: 'Andrea',
}))),
rest.get('/brands', (req, res, ctx) => res(ctx.json(brands))),
);
worker.start();
react page
import React, { useState, useEffect } from 'react';
import { BrandsAPIsSingleton } from '../../modules/api';
// components
import BrandsMainTable from './table/BrandsMainTable';
export default function Main() {
const [isLoading, setIsLoading] = useState([]);
const [brands, setBrands] = useState([]);
useEffect(() => {
setIsLoading(true);
BrandsAPIsSingleton.getAllBrands()
.then((r) => {
setBrands(r.data);
setIsLoading(false);
}).catch((error) => {
setIsLoading(false);
console.log(error);
});
}, []);
if (isLoading) {
return <div>Loading...</div>;
}
return (
<BrandsMainTable data={brands} />
);
}
Anybody else had this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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