Skip to content

feat(connector): provide prev state slice to target (function) #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2018
Merged

feat(connector): provide prev state slice to target (function) #171

merged 2 commits into from
Apr 2, 2018

Conversation

AKolodeev
Copy link
Contributor

@AKolodeev AKolodeev commented Oct 14, 2017

Hi! First of all, thank you for your work! This library is really useful.

Sometimes I pass a callback function as target and I need to compare previous state slice with a new one. I can achieve this such way:

import { pick } from 'lodash';

mergeToTarget(nextState, actions) {
  const prevState = pick(this, Object.keys(nextState));

  if (prevState.foo !== nextState.foo) {
    this.handleFooChange(nextState.foo);
  }

  Object.assign(this, nextState);
  Object.assign(this, actions);
}

But I sure it can be achieved much more easily.

In this PR I provide previous state slice as third argument for target function.
For initial update I provide undefined.

Also, I fixed store reducer in connector.spec.js: because of redux initial action defaultState.baz would be undefined instead of -1.

@AKolodeev
Copy link
Contributor Author

AKolodeev commented Nov 30, 2017

@AntJanus? Or anybody?

@AntJanus
Copy link
Collaborator

I'm a fan of this but I'm wondering if there is an equivalent in the react-redux library. My vision for ng-redux has been to provide as much parity as possible without providing many extra features.

@AntJanus
Copy link
Collaborator

Sorry for the late response on this. Work + kids have kept me busy and unable to dedicate my time to the library. I'm hoping to pick it back up after the holidays! :)

@AKolodeev
Copy link
Contributor Author

AKolodeev commented Dec 1, 2017

React components have componentWillReceiveProps \ componentDidReceiveProps hooks which will get next props include props from mapStateToProps function and have access to previous props. In AngularJS app we can't use $onChanges hook to detect state changes caused by mapStateToTarget function. So I think, the most convenient way is provide prevState in mapStateToTarget.

Thank you for answer :)

@AntJanus
Copy link
Collaborator

AntJanus commented Dec 1, 2017

@AKolodeev fantastic! I'll put it on my todo list to test this out and I'll merge it in as soon as I can.

@AKolodeev
Copy link
Contributor Author

Fixed conflicts. @AntJanus will you merge it? :)

@AntJanus AntJanus merged commit f9494cc into angular-redux:master Apr 2, 2018
@AKolodeev AKolodeev deleted the feat/provide-prev-state-slice branch April 8, 2018 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants