Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import React, { Component } from 'react';
  2.  
  3. // NOTE: If you override willMount or unMount functions, it will not update state.
  4.  
  5. export default class MountedComponent extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.callbacks = [];
  9. this.mounted = false;
  10. }
  11. componentWillMount() {
  12. this.mounted = true;
  13. this.stateCallbackCheck();
  14. }
  15. componentWillUnmount() {
  16. this.mounted = false;
  17. }
  18. stateCallbackCheck() {
  19. let callback;
  20. if (this.mounted) {
  21. while (this.callbacks.length > 0) {
  22. callback = this.callbacks.splice(0, 1)[0];
  23. super.setState.apply(callback[0], callback[1]);
  24. }
  25. }
  26. }
  27. setState() {
  28. this.callbacks.push([this, arguments]);
  29. this.stateCallbackCheck();
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement