Fra*_*n M 5 reactjs react-redux react-hooks
// 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 ]);
Run Code Online (Sandbox Code Playgroud)
我的问题是,每当我调用 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
}, [ ]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4827 次 |
| 最近记录: |