问题
功能组件导致多个重新呈现,从而导致多个ajax请求。解决方案是什么?
这是我的密码
export default function MyMenu() {
let menu = useStoreState(state => state.menu.menu);
const getMenu = useStoreActions(actions => actions.menu.getMenu);
let categoryId = useStoreState(state => state);
const setCategoryId = useStoreActions(actions => actions.menu.setCategoryId);
const [localCategoryId, setLocalCategoryId] = React.useState(0);
React.useEffect(() => {
getMenu();
});
// below is usual return method
}
您应该将一个依赖项数组作为第二个参数传递给useEffect
。如果你希望它只运行一次-传递一个空数组,如下所示:
React.useEffect(() => {
getMenu();
}, []);
缺少第二个参数[]
,请查看:
React.useEffect(() => {
getMenu();
}, []);