// 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
}, [ ]);
好吧,我想通了,我在 Reducer 中的一个类型将 de 铭文搁置到和空数组,所以它无限地重新渲染。