BaseControl.jsx
Home
/
FileServer /
SPA /
src /
JS /
React /
BaseControl.jsx
import React from 'react';
import autoBind from 'react-autobind';
import ControlRegister from '../Registers/ControlRegister'
import EventRegister from '../Registers/EventRegister'
import { ControlLog as Log } from '../Tools/LogTools'
export default class BaseControl extends React.Component {
static controlRegister = ControlRegister.Get();
static eventRegister = EventRegister.Get();
//Глобальные данные приложения
static GlobalState = {};
constructor(props, name) {
super(props);
this.Name = name;
autoBind(this);
this.GlobalState = BaseControl.GlobalState;
this.EventRegister = BaseControl.eventRegister;
this._isMounted = false;
//Функция логирования для текущего объекта
this.Log = (Msg) => { Log(this, Msg); };
//Регистрация объекта в реестре контролов
BaseControl.controlRegister.RegisterControl(this);
this.Log("start");
}
//При выполнения promise (запроса к api)
//копмонент инициатор запроса может быть уничтожен (componentDidMount)
//В данном случае выполнение then(...this.setState(...)...).bind(this) считается некорректным
setState(new_state) {
if (!this._isMounted) {
return;
}
super.setState(new_state);
}
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
//console.log(this.name + " stop " + this.ID);
//Удаление из реестра контролов
BaseControl.controlRegister.UnregisterControl(this);
//Если компонент был подписан на события, то удаление из реестра слушателей событий
if (this.IsEventListener === true) {
BaseControl.eventRegister.UnregistListener(this);
}
}
}