-
Notifications
You must be signed in to change notification settings - Fork 1.3k
mapPropsOnChange #60
Comments
The mapPropsOnChange(
['foo', 'bar'],
({ foo, bar, ...rest }) => ({
foobar: computationallyIntensiveFunction(foo, bar) // computed prop
...rest // rest of props
}),
BaseComponent
) |
This will not work! |
As I wrote the only way to make it work is to combine props with childProps at render
|
Oh... I see what you mean now. Need to write a failing test case. |
But looks like mapPropsOnChange is the good name for current behavior, |
Actually, probably need to rethink |
Sounds good, thank you! |
I dunno about |
Maybe we can avoid this confusion by having the |
This will work. There is no need in computation to use nondependent props. Like this? import { Component } from 'react'
import pick from 'lodash/object/pick'
import omit from 'lodash/object/omit'
import shallowEqual from 'recompose/shallowEqual'
import createHelper from 'recompose/createHelper'
import createElement from 'recompose/createElement'
const mapPropsOnChange = (depdendentPropKeys, propsMapper, BaseComponent) => {
const pickDependentProps = props => pick(props, depdendentPropKeys)
const omitDependentProps = props => omit(props, depdendentPropKeys)
return class extends Component {
childProps = propsMapper(pickDependentProps(this.props))
componentWillReceiveProps(nextProps) {
if (!shallowEqual(
pickDependentProps(this.props),
pickDependentProps(nextProps)
)) {
this.childProps = propsMapper(pickDependentProps(nextProps))
}
}
render() {
return createElement(BaseComponent, {...omitDependentProps(this.props), ...this.childProps})
}
}
}
export default createHelper(mapPropsOnChange, 'mapPropsOnChange') |
Yes except I'd change the implementation a bit so that this.childProps = {
...propsMapper(pickDependentProps(nextProps)),
...omitDependentProps(nextProps)
} |
You means props order, not moving omitted props into childProps? |
Oh duh, yes |
Forget my previous comment |
dependent props. Resulting object is combined with non-dependent props. Closes #60
👍 Thank you!!! |
recompose does not allow to use most used pattern similar to
mapPropsOnChange
What if I need to create computationally intensive work only if some props is changed, but pass other props as is. So I get all props online, and compute results of intensive tasks only when it needed.
For example I have property
x
anddata
, I need to make some intensive calculus ifdata
changed but also I need to get current version ofx
in my component. WithmapPropsOnChange
I get the value ofx
on the momentdata
has changed. But every time I use this pattern I need newx
values.This could be solved creating a HOC based on
mapPropsOnChange
but changing this linehttps://github.com/acdlite/recompose/blob/master/src/packages/recompose/mapPropsOnChange.js#L23
on this
What are you think about
HOC with name and signature
The text was updated successfully, but these errors were encountered: