功能组件导致多次重新渲染,导致多次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
}
答案 0 :(得分:1)
您应该将依赖项数组作为第二个参数传递给 useEffect
。如果您希望它只运行一次 - 传递一个空数组,如下所示:
React.useEffect(() => {
getMenu();
}, []);
答案 1 :(得分:1)
缺少第二个参数 []
,看一下:
React.useEffect(() => {
getMenu();
}, []);