GithubHelp home page GithubHelp logo

mapPropsOnChange about recompose HOT 15 CLOSED

acdlite avatar acdlite commented on June 19, 2024
mapPropsOnChange

from recompose.

Comments (15)

acdlite avatar acdlite commented on June 19, 2024

The propsMapper parameter of mapPropsOnChange() receives the entire props object, not just the dependent props. So you can achieve what you're looking for like so:

mapPropsOnChange(
  ['foo', 'bar'],
  ({ foo, bar, ...rest }) => ({
    foobar: computationallyIntensiveFunction(foo, bar) // computed prop
    ...rest // rest of props
  }),
  BaseComponent
)

from recompose.

istarkov avatar istarkov commented on June 19, 2024

This will not work!
propsMapper will be called only if foo or bar has changed,
but if changed something in the rest you will got the old childProps

from recompose.

istarkov avatar istarkov commented on June 19, 2024

As I wrote the only way to make it work is to combine props with childProps at render

return createElement(BaseComponent, {...this.props, ...this.childProps})

from recompose.

acdlite avatar acdlite commented on June 19, 2024

Oh... I see what you mean now. Need to write a failing test case.

from recompose.

istarkov avatar istarkov commented on June 19, 2024

But looks like mapPropsOnChange is the good name for current behavior,
it looks like for new behavior it will be better to create something like withPropsOnChange?

from recompose.

acdlite avatar acdlite commented on June 19, 2024

Actually, probably need to rethink mapPropsOnChange() entirely. I don't like mapping the entire props object, but then mixing it back into the original props object.

from recompose.

istarkov avatar istarkov commented on June 19, 2024

Sounds good, thank you!

from recompose.

acdlite avatar acdlite commented on June 19, 2024

I dunno about withPropsOnChange() either... could work.

from recompose.

acdlite avatar acdlite commented on June 19, 2024

Maybe we can avoid this confusion by having the propsMapper not receive the entire props object, only the dependent props. Then combine with the rest of the props at the end.

from recompose.

istarkov avatar istarkov commented on June 19, 2024

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')

from recompose.

acdlite avatar acdlite commented on June 19, 2024

Yes except I'd change the implementation a bit so that

this.childProps = {
  ...propsMapper(pickDependentProps(nextProps)),
  ...omitDependentProps(nextProps)
}

from recompose.

istarkov avatar istarkov commented on June 19, 2024

You means props order, not moving omitted props into childProps?
(moving props into childProps we got something like old behavior)

from recompose.

acdlite avatar acdlite commented on June 19, 2024

Oh duh, yes

from recompose.

acdlite avatar acdlite commented on June 19, 2024

Forget my previous comment

from recompose.

istarkov avatar istarkov commented on June 19, 2024

👍 Thank you!!!

from recompose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.