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

124
Views
React - Reducing context data size using multiple contexts

INTRODUCTION

In my app I have multiple contexts for managing and storing state.

In one of my scenarios, I have noticed that, as I have a UsersContext (which stores the users data), it is not necessary to store the user data of each post creator in my PostsContext, it could be better (for RAM) to just store its user id, and not to repeat the user data on it.

I have thought to use "parsers" inside my reducers logic before storing data, in order to remove the unnecessary fields of each object.

Is this a good idea / is a common pattern?

PROBLEM

The main problem I notice with this approach is: how to get the data correctly from both contexts? I mean, imagine the following component:

function Card({ content: { userData, image, description, location, date }) {
   ...
}

If I want to get the data from the contexts, and not from props, I have implemented two custom hooks (do not care about memoizations right now) which consumes a specific context:

/* HOOKS FOR GETTING SPECIFIC DATA FROM CONTEXT */

function useUpdatedPostData(postData) {
    const posts = usePosts(); <-- consume PostsContext

    return {
       ...postData,
       ...posts.getPost(postData.id) <--- Merging with data from context 
    }
}

function useUpdatedUserData(userData) {
    const users = useUsers(); <-- consume UsersContext

    return {
       ...userData,
       ...users.getUser(userData.id) <--- Merging with data from context 
    }
}


/* CONSUMER COMPONENT */

function Card({ content }) {
    // The main problem: ​
   ​const { 
      ​image,
      ​description,
      ​location,
      ​date,
   ​} = useUpdatedPostData(content);

  const { username, avatar } = useUpdatedUserData(content.userData);

  ...

}

something which makes the code really difficult to read. Any tips?

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!