I'm displaying three user lists. They're filtered by status. These are my active users, inactive, and pending. I'm doing it by using a simple user list component that I wrote, and filtering the users' array based on the status.
I'd like to replace this block of code. I want to hide away the custom filtering logic into some custom components. Instead, we'll have three components. We'll have active users. I just want to pass it to users and have it figure out which ones are active.
We'll have inactive users, and we'll have pending users. I'll create active users by calling a higher-order component called Filter by Status. I'm going to tell it I want to filter by the active status and I'll pass it my user list, which is the base component that I want to modify. I'll do the same thing for inactive users and pending users.
Now, let's implement Filter by Status. Filter by Status needs to take in a status, and it needs to return a higher-order component. I'm going to make use of the map-props higher-order component from Recompose.
Map-props takes in a function. That function takes in the props. We'll take some props in and we'll map them to some new props. The only prop I care to look at is the user's prop.
I want to return an object, which will be the new prop object. Map-props completely blows away any props passed in. If they're not in this object, they won't be passed to the wrapped component.
I want to pass in status and I want to pass in a new user's prop. This one will take the user's prop coming in, but filter it the same way we were doing before. I'll check if it's equal to the status that I supply.
User list now gets the filtered users and the status. I want to add the header into my component. I'll use the status and the word users to create my header. When I refresh, everything continues to behave the same.
If I were to change one of the user's statuses, you'll see the filtering logic continues to work.