Add useGetState hook

This commit is contained in:
Alex Gleason 2023-03-13 17:45:35 -05:00
parent a19b1e83a9
commit 8547aeb517
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 18 additions and 6 deletions

View file

@ -5,6 +5,7 @@ export { useAppSelector } from './useAppSelector';
export { useClickOutside } from './useClickOutside';
export { useCompose } from './useCompose';
export { useDebounce } from './useDebounce';
export { useGetState } from './useGetState';
export { useGroup, useGroups } from './useGroups';
export { useGroupsPath } from './useGroupsPath';
export { useDimensions } from './useDimensions';

View file

@ -1,12 +1,9 @@
import api from 'soapbox/api';
import { useAppDispatch } from './useAppDispatch';
import { useGetState } from './useGetState';
/** Use stateful Axios client with auth from Redux. */
export const useApi = () => {
const dispatch = useAppDispatch();
return dispatch((_dispatch, getState) => {
return api(getState);
});
const getState = useGetState();
return api(getState);
};

View file

@ -0,0 +1,14 @@
import { useAppDispatch } from './useAppDispatch';
import type { RootState } from 'soapbox/store';
/**
* Provides a `getState()` function to hooks.
* You should prefer `useAppSelector` when possible.
*/
function useGetState() {
const dispatch = useAppDispatch();
return () => dispatch((_, getState: () => RootState) => getState());
}
export { useGetState };