Imagina que tienes el siguiente contexto
contextos/mensajes/UserPostsContext.js
const UserPostsContext = createContext(null); export default UserPostsContext; export function UserPostsProvider({ children }) { const [posts, dispatch] = useReducer(userPostsReducer, initialState); const addUserPosts = (userId, posts, unshift = false) => { dispatch(actionCreators.addUserPosts(userId, posts, unshift)); }; const deleteUserPost = (userId, postId) => { dispatch(actionCreators.deleteUserPost(userId, postId)); }; const getUserPosts = (userId) => posts[userId] ?? []; return ( <UserPostsContext.Provider value={{ addUserPosts, deleteUserPost, getUserPosts, }} > {children} </UserPostsContext.Provider> ); }Donde simplemente agrega/elimina/obtiene publicaciones que se han obtenido en un gancho personalizado
usarFetchUserPosts.js
const useFetchUserPosts = (() => { const mutex = {}; const pagination = {}; const listeners = {}; return (userId) => { const userPosts = useContext(UserPostsContext); const posts = userPosts.getUserPosts(userId); const [isLoading, setIsLoading] = useState(!posts.length); ... const getMorePosts = async (limit = MAX_USER_POSTS_TO_RETRIEVE) => { if ( mutex[userId]?.isFetching || pagination[userId]?.hasMoreToLoad === false || !isMounted() ) { return; } ... const newPosts = await api.getUserPosts(userId); userPosts.addUserPosts(userId, newPosts); ... } } })();Mi pregunta es, dado que me tomo muy en serio la delegación de responsabilidades en el código para que sea lo más escalable y legible posible, ¿podría usarse la API React Context para obtener apis en lugar de ganchos?
Quiero decir, para mí, contexts tiene la única funcionalidad de "administrar estados" globalmente, y no debe usarse para realizar solicitudes de API y cosas por el estilo, pero he visto que es común en algunas personas delegar esta parte a React. Contextos.
¿No es esto un anti-patrón o algo así?