Recompose: Flatten Prop without ommiting prop

Created on 5 Jun 2016  路  3Comments  路  Source: acdlite/recompose

Using flattenProp flattens the property, and recompose (intuitively) removes the flattened property.

I find however, that more often than not I need to access the base, unflattened prop while also appreciating the convenience of a flattened prop.

As an example, consider a Relay container like so:

 fragment on Item {
  fieldOne,
  fieldTwo
  ${SubItem.getFragment('item')}
}

and a component:

<div>
  <div>{fieldOne}</div>
  <div>{fieldTwo}</div>
  <SubItem item={item} />
</div>

Using flattenProp prevents me from doing the above, and I need to instead opt for the uglier:

<div>
  <div>{item.fieldOne}</div>
  <div>{item.fieldTwo}</div>
  <SubItem item={item} />
</div>

It would be great if it is possible to flattenProp without omitting the actual property. It can even be an optional argument to flattenProp to maintain backwards compatibility, or even a new HOC if deemed necessary.

I'm not sure if there are complications that might arise from this, or if there are already existing ways to do this with the current API.

good first issue

Most helpful comment

With modern js it's not a big deal to write flattenProp you need using mapProps.

mapProps(({ ...props, item }) => ({ ...props, ...item, item }))

All 3 comments

With modern js it's not a big deal to write flattenProp you need using mapProps.

mapProps(({ ...props, item }) => ({ ...props, ...item, item }))

This is a good point. I can't imagine having the original prop stick around is ever a problem.

Closed by #195.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uriklar picture uriklar  路  4Comments

istarkov picture istarkov  路  3Comments

astanciu picture astanciu  路  3Comments

rockchalkwushock picture rockchalkwushock  路  3Comments

jethrolarson picture jethrolarson  路  4Comments