Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component } from 'react';
- // NOTE: If you override willMount or unMount functions, it will not update state.
- export default class MountedComponent extends Component {
- constructor(props) {
- super(props);
- this.callbacks = [];
- this.mounted = false;
- }
- componentWillMount() {
- this.mounted = true;
- this.stateCallbackCheck();
- }
- componentWillUnmount() {
- this.mounted = false;
- }
- stateCallbackCheck() {
- let callback;
- if (this.mounted) {
- while (this.callbacks.length > 0) {
- callback = this.callbacks.splice(0, 1)[0];
- super.setState.apply(callback[0], callback[1]);
- }
- }
- }
- setState() {
- this.callbacks.push([this, arguments]);
- this.stateCallbackCheck();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement