// get state
const { inscriptions } = useSelector( state => state.user );
// flag
const instances = Object.keys(inscriptions).length;
// dispatch
const dispatch = useDispatch();
const getInscriptions = () => dispatch( getInscriptionsAction() );
useEffect( () => {
// call api only if empty
if(instances === 0) {
const queryToAPI = async () => {
getInscriptions();
}
queryToAPI();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ instances, inscriptions ]);
我的问题是,每当我在 de useEffect 中调用强制存储更新的 API 时,这会使组件重新渲染,从而启动无限循环。我不能if(isntances === 0) return null;在 useEffect 下放置类似的东西,因为我的铭文数组可以是空的,我尝试添加各种标志,但它保持无限循环。有任何想法吗?
==================编辑=============================== = 好的,现在我已经实现了一些建议,但问题仍然存在,无限循环仍然存在。
// get state
const { inscriptions } = useSelector( state => state.user );
// flag
const instances = Object.keys(inscriptions).length;
// dispatch
const dispatch = useDispatch();
// const getInscriptions = () => dispatch( getInscriptionsAction() );
const getInscriptions = useCallback(
() => dispatch( getInscriptionsAction() ),
[ dispatch, getInscriptionsAction ]
);
useEffect( () => {
// call api only if empty
if(instances === 0) {
// const queryToAPI = async () => {
getInscriptions();
// }
// queryToAPI();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ ]);