Tracker.autorun запускается только один раз
Вот с чем я сейчас работаю
class FooComponent extends Component {
constructor(...args) {
super(...args);
this.state = {
model: this.getModel()
};
}
componentWillUnmount() {
this._unmounted = true;
this._modelComputation && this._modelComputation.stop();
super.componentWillUnmount && super.componentWillUnmount();
}
getModel() {
const model = {};
this._modelComputation && this._modelComputation.stop();
this._modelComputation = Tracker.autorun((computation) => {
const { id } = this.props;
const data = id && Collection.findOne(id);
if (data) {
Object.assign(model, data);
!this._unmounted && this.forceUpdate();
}
});
return model;
}
...
}
К сожалению, реактивная модель не работает, и Tracker.autorun
не выполняет функцию при обновлении модели в базе данных. Из документации, Collection.findOne
должен быть реактивным, верно?
Я не уверен, что я делаю неправильно. Почему нет Tracker
мониторинг модели БД? Почему функция не переоценивает Collection.findOne
когда БД меняется?
** Редактировать **
При обновлении БД я вижу изменение коллекции через meteortoys:allthings
, но autorun
не повторяется
1 ответ
Решение
Смотря как tracker-react
реализует его, я изменил свой код так
class FooComponent extends Component {
constructor(...args) {
super(...args);
this.state = {
model: this.getModel()
};
}
componentWillUnmount() {
this._unmounted = true;
this._modelComputation && this._modelComputation.stop();
super.componentWillUnmount && super.componentWillUnmount();
}
getModel() {
const model = {};
this._modelComputation && this._modelComputation.stop();
this._modelComputation = Tracker.nonreactive(() => {
return Tracker.autorun((computation) => {
const { id } = this.props;
const data = id && Collection.findOne(id);
if (data) {
Object.assign(model, data);
!this._unmounted && this.forceUpdate();
}
});
});
return model;
}
...
}
Я не до конца понимаю почему, но сейчас это работает.