G
G
Grigory Dikiy2016-08-25 21:46:05
JavaScript
Grigory Dikiy, 2016-08-25 21:46:05

ComponentWillMount not changing property?

Hello, I don't understand what I'm doing wrong. There is a component in which properties are written: componentWillMount, componentWillUnmount, where the this.mount property is simply changed to true or false. There is a callBack function for the timer that operates on this property, but for some reason it does not correctly read this.mount if you moved to another section and returned back (in the log it shows that this.mount is false), although I make sure that componentWillMount works and changes it, what could be the reason?

import React from 'react';
import { connect } from 'react-redux';

import TimerComponent from '../components/TimerComponent';
import { setTime, startBreak, finishBreak, startTimer, finishTimer, tickTimer, skipTimer, pauseTimer, resumeTimer } from '../actions/TimerActions';
import { createNewTask } from '../actions/TaskActions';
import { transformTimeFromSeconds, createNowDate } from '../utils/DateTime';
import { CONST } from  '../constants/TimerConstants';
import Timer from '../utils/Timer';

class TimerContainer extends React.Component {
  constructor(props) {
    super(props);

    this.timer = new Timer(0, this.props.timeout);
    this.notification = new Audio('notification.mp3');
    this.mount = true;
  }

  componentWillMount() {
    this.mount = true;
  }

  componentWillUnmount() {
    this.mount = false;
  }

  ...
  updateTime(time) {
    if(time === 0) this.props.prevStatus === CONST.START_TIMER ? this.finishTimer() : this.finishBreak();
    if(this.mount) this.forceUpdate(); // Функция принудительного рендеринга не вызывается после возвращения из другого раздела
  }

  getTimePrecent() {
    return (this.timer.getFullTime() - this.timer.getCurrentTime()) * 100 / this.timer.getFullTime();
  }

  render() {
    var time = this.props.status === CONST.TICK_TIMER ? this.timer.getCurrentTime() : this.timer.getFullTime();
    var percent = this.getTimePrecent();

    return (
      <TimerComponent
        status={this.props.status}
        time={time}
        percent={percent}
        startTimer={this.startTimer.bind(this)}
        skipTimer={this.skipTimer.bind(this)}
        pauseTimer={this.pauseTimer.bind(this)}
        resumeTimer={this.resumeTimer.bind(this)}
        startBreak={this.startBreak.bind(this)}
        />
    );
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nd0ut, 2016-08-25
@frilix

Where and how is updateTime called? Maybe you didn’t pass the context of the current component into it and it will be called in the context of something else? What does this point to inside updateTime?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question