项目作者: MAKARD

项目描述 :
React-redux lifeCycle callbacks
高级语言: TypeScript
项目地址: git://github.com/MAKARD/react-redux-lifecycles.git
创建时间: 2018-09-25T10:10:03Z
项目社区:https://github.com/MAKARD/react-redux-lifecycles

开源协议:MIT License

下载


React Redux LifeCycles

Build Status
codecov

Store lifeCycle callbacks (will be extended in future):

  1. storeDidUpdate(storeState: YourStoreStateInterface): void {}
  2. storeDidUpdateState(currentStoreState: YourStoreStateInterface, prevStoreState: YourStoreStateInterface): void {}

Install

Via npm

  1. npm install --save react-redux-lifecycles

Usage

As decorator:
  1. import * as React from "react";
  2. import { connect } from "react-redux";
  3. import { withLifeCycles } from "react-redux-lifecycles";
  4. @withLifeCycles()
  5. @connect(mapStateToProps, mapDispatchToProps, mergeOptions, { withRef: true })
  6. export class Component extends React.Component {}
As HOC:
  1. import * as React from "react";
  2. import { connect } from "react-redux";
  3. import { withLifeCycles } from "react-redux-lifecycles";
  4. class Component extends React.Component {}
  5. export default withLifeCycles()(connect(mapStateToProps, mapDispatchToProps, mergeOptions, { withRef: true })(Component));

Note: withLifeCycles apply as argument only connected component

Note: You must pass { withRef: true } argument to connect method

Methods

storeDidUpdate

Will called after store received new state (reducer has been executed).

To get access to storeDidUpdate method just use withLifeCycles() method without any arguments.

storeDidUpdateState

Will called after store received new specific state (reducer has been executed).

To get access to storeDidUpdateState method just use withLifeCycles() with array of strings as argument.
For example:

  1. /**
  2. storeState: {
  3. field1: {
  4. level1: {
  5. level2: true
  6. },
  7. subLevel: false
  8. },
  9. field2: {
  10. level1: false
  11. },
  12. loading: false
  13. }
  14. **/
  15. // storeDidUpdateState will be executed if one of the passed arguments changed
  16. @withLifeCycles(["field1.level1.level2", "loading"])
  17. @connect(mapStateToProps, mapDispatchToProps, mergeOptions, { withRef: true })
  18. export class Component extends React.Component {
  19. storeDidUpdateState(currentState) {
  20. /**
  21. if reducer change only 'field2' or 'field1.subLevel' value, 'storeDidUpdateState' does not be executed
  22. if reducer change 'loading' or 'field1.level1.level2' value, 'storeDidUpdateState' will be executed
  23. **/
  24. }
  25. storeDidUpdate(currentStoreState, prevStoreState) {
  26. // Always executing after store received new state
  27. }
  28. }