我正在尝试为获取对象的组件设置默认道具。问题是我需要将默认属性应用于对象缺少的属性,但它仅在未设置整个对象时才有效:
const App = ({data}) =>{
return(
<div></div>
)
}
App.defaultProps = {
data : {
C : "blah"
}
}
App.propTypes = {
data : PropTypes.shape({
A : PropTypes.string.isRequired,
B : PropTypes.string.isRequired,
C : PropTypes.string.isRequired,
}).isRequired
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有这个传奇效果,它调用 API 并在成功时分派一个操作:
export function* getThemEffect() {
try {
yield put(requestActoin());
const data: AxiosResponse<ServerResponseSchema> = yield call(getStuff);
yield put(successAction(data.data.data));
} catch (err: any) {
yield put(failureAction(err?.response?.data || null));
}
}
Run Code Online (Sandbox Code Playgroud)
这是辅助函数:
export function getStuff() {
const config: AxiosRequestConfig = {
method: "GET",
url: "https://somewhere.com/api/get"
};
return axios(config);
}
Run Code Online (Sandbox Code Playgroud)
这个传奇的测试服看起来像这样:
import * as api from "../api";
const getStuffSpy = jest.spyOn(api, "getStuff");
describe("search saga", () => {
let gen: Generator, response: any, getStuffMock: jest.Mock;
beforeEach(() => {
getStuffSpy.mockClear();
gen = getThemEffect();
getStuffMock …Run Code Online (Sandbox Code Playgroud)