Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import React from 'react';
  2.  
  3. const original = React.Component.prototype.setState;
  4.  
  5. const logState = function (type, current, updated) {
  6. console.log(type.displayName || type.name || 'Component', 'updating', current, 'with', updated);
  7. }
  8.  
  9. const logger = function (partialState, callback) {
  10. const type = this._reactInternalInstance._currentElement.type;
  11. if (typeof partialState === 'function') {
  12. const partialStateCallback = partialState;
  13. partialState = function (state, props, context) {
  14. const updated = partialStateCallback(state, props, context);
  15. logState(type, state, updated);
  16. }
  17. } else if (partialState) {
  18. logState(type, this.state, partialState);
  19. }
  20.  
  21. original.call(this, partialState, callback);
  22. };
  23.  
  24.  
  25. function hijack () {
  26. React.Component.prototype.setState = logger;
  27. }
  28.  
  29. function restore () {
  30. React.Component.prototype.setState = original;
  31. }
  32.  
  33. export {
  34. hijack,
  35. restore
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement