Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Flatten a Prop using Recompose

    Tim KindbergTim Kindberg

    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.

    reactReact
    ^16.0.0
    recomposeRecompose
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss