1

I have a redux store like so:

 {
  '1f458697-e2c0-4d11-ada0-ee7b113c2429': {
    name: 'Jim',
    title: 'Developer',
    active: true
  },
  '4498eb08-3786-495a-a66c-21a65138ab24': {
    name: 'Darren',
    title: 'Human Resources',
    active: false
  },
  'c7975551-153f-4eab-a875-ed5445f76252': {
    name: 'Francesa',
    title: 'Chief of Internal Operations',
    active: false
  }
}

And I have a selector that gets an active user.

export const getActiveUser = (users) => _.find(users, { 'active': true });

My question is, whats the nicest way to include the ID of the user in the payload to use within my React component:

  1. Is this is a bad practice and a sign that I should be using an array instead.

  2. Should I just change my user payload to include the ID. This feels wrong as I would be duplicating data.

  3. Should I manipulate that specific selector to include the users ID.

2 Answers 2

1

Yes, use arrays for table-like data and filter function for filtering the result set (if multiple users can be active at a time). If only one user can be active at a time, consider changing the db schema and create a separate variable activeUserId instead of having all of them contain unnecessary payload (and change the shape of the redux store accordingly).

Sign up to request clarification or add additional context in comments.

Comments

0

- Is this is a bad practice and a sign that I should be using an array instead.

Normalizing the data, so you can edit or delete items (users in your case) in O(1) is a good approach, which is also covered in the Dan Abramov's idiomatic redux course.

- Should I just change my user payload to include the ID. This feels wrong as I would be duplicating data.

Since you don't usually change IDs, adding them inside the user's object should not be a problem, and it should give you quick access to the ID. However, if you don't won't to duplicate that's acceptable as well.

- Should I manipulate that specific selector to include the users ID.

Part of the the selectors role is to create derived data, just like what you do when you filter the users. So if you want to the the ID to each user, this is a good place to do so. Don't mutate the objects, just create new user objects with ID. However, if you've got lots of users, you should use memoized selectors or add the ID as part of the payload to prevent performance issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.