Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Reaccionar: obtenga datos paginados del contexto

tengo el contexto actual

 export function UserPostsProvider({ children }) { const [posts, dispatch] = useReducer(userPostsReducer, initialState); const addUserPosts = (userId, posts, unshift = false) => { dispatch(actionCreators.addUserPosts(userId, posts, unshift)); }; const getUserPosts = (userId, index = 0, limit = undefined) => { const _posts = posts[userId] ?? []; return _posts.slice(index, limit + index || undefined); }; return ( <UserPostsContext.Provider value={{ addUserPosts, deleteUserPost, getUserPosts, }} > {children} </UserPostsContext.Provider> ); }

Como puede ver, tengo un método "getUserPosts" que acepta dos argumentos "índice" y "límite".

Estoy consumiendo este contexto de la siguiente manera:

 export default function useFetchUserPosts(userData) { const isMounted = useIsMounted(); const userPosts = useUserPosts(); const cards = useCards(); const posts = userPosts.getUserPosts(userData.id); // <--- HERE! const [isLoading, setIsLoading] = useState(!posts.length); const [error, serError] = useState(undefined); const startAfter = useRef(new Date()); const getMorePosts = async (limit = 10) => { if (isFetching.current || !hasMoreToLoad.current || !isMounted()) { return; } isFetching.current = true; setIsLoading(true); try { const { posts: newPosts, hasMoreToLoad: newHasMoreToLoad, startAfter: newStartAfter, } = await getUserPosts(userData.id, startAfter.current, limit); // The component might be unmounted if (!isMounted()) return; hasMoreToLoad.current = newHasMoreToLoad; startAfter.current = newStartAfter; userPosts.addUserPosts(userData.id, newPosts); setIsLoading(false); isFetching.current = false; } catch(err) { setError(err); } } ... }

¿Cómo puedo hacer para obtener la sección paginada correcta de las publicaciones del contexto? Quiero decir, si solo obtengo las primeras 10 publicaciones del contexto en cada nueva representación, siempre recibiré las primeras 10 publicaciones, no más que eso.

¿Alguna ayuda?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!