Flatten a Prop using Recompose

Tim Kindberg
InstructorTim Kindberg
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

Learn how to use the ‘flattenProp’ higher order component to take a single object prop and spread each of its fields out as a prop.

[00:00] I have this example where I've connected this user component to a fake Redux state. I'm mapping the user portion of the state into my component as the user prop and I'm printing it on the screen with user.name and user.status.

[00:14] I'd like to be able to flatten it out so that I can ask for name and status as their own props, so I'll delete user. in both these instances.

[00:26] I can take this connect call and move it into a new higher-order component where I'll compose the connect call and a usage of flattened prop. Flattened prop just takes one param, the name of the prop that you'd like to flatten. In this case, it's going to be user.

[00:51] When I use my new higher-order component here, it's going to, first, connect to the Redux state and pull out the user, and then it's going to take that user and flatten it out so that all of its fields are spread into my wrapped component. When I refresh, it works just as before.