Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1083 of the free egghead.io lessons, plus get React content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Flatten a Prop using Recompose

1:10 React lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

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.

const mapStateToProps = (state) => ({ user: state.user });

const User  = connect(mapStateToProps)(({ user }) =>
    <div className="User"> { user.name } - { user.status } </div>
);

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.

const User  = connect(mapStateToProps)(({ user, status }) =>
    <div className="User"> { name } - { status } </div>
);

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 flattenProp. flattenProp just takes one param, the name of the prop that you'd like to flatten. In this case, it's going to be user.

const enhance = compose(
    connect(mapStateToProps),
    flattenProp('user')
);

When I use my new higher-order component here in User, 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.

const User  = enhance(({ user, status }) =>
    <div className="User"> { name } - { status } </div>
);


HEY, QUICK QUESTION!
Joel's Head
Why are we asking?