2016-10-04 2 views
0

나는 네이티브 반응을 배우려고 노력하고 있지만 클래스 변수와 관련된 문제로 고민하고 있습니다. 이것은 (매우 긴이기 때문에 전체를 붙여하지 않았다, 그러나 이것은 내 문제의 아이디어를 제공해야합니다) 내 코드입니다 : "_currentTime클래스 변수에있는 소품이 정의되지 않았습니다.

class VideoScreen extends Component { 
static _toHHMMSS(time) { 
    var sec_num = parseInt(time, 10); // don't forget the second param 
    var hours = Math.floor(sec_num/3600); 
    var minutes = Math.floor((sec_num - (hours * 3600))/60); 
    var seconds = sec_num - (hours * 3600) - (minutes * 60); 

    if (hours < 10) { 
     hours = "0" + hours; 
    } 
    if (minutes < 10) { 
     minutes = "0" + minutes; 
    } 
    if (seconds < 10) { 
     seconds = "0" + seconds; 
    } 
    return hours + ':' + minutes + ':' + seconds; 
} 

_currentTime = <View refreshing> 
    <Text>{VideoScreen._toHHMMSS(this.props.currentTime)} 
    </Text> 
</View>; 

render() { 
    return (
     <View refreshing> 
      {this._currentTime} 
     </View> 
    ) 
} 

}

불행히도 변수 내에서 정의되지 소품 ". 뷰를 render 메서드로 직접 이동하면 잘 동작합니다 (부울 값에 따라 코드를 숨기고 변수를 만들어야 코드가 훨씬 더 읽기 쉬워지기 때문에 별개의 변수에서 필요합니다). 메소드를 사용하는 것처럼 변수를 클래스에 바인딩해야합니까, 아니면 다른 것을 잘못하고 있습니까?

답변

0

문제는 this이 바인딩되어 있지 않습니다. 변수를 바인딩 할 수 있는지는 잘 모르지만 메소드로 원하는 것을 얻을 수 있습니다.

class VideoScreen extends Component { 
    constructor(props) { 
     super(props); 
     this._currentTime = this._currentTime.bind(this); 
    } 

    render() { 
     return (
      <View refreshing> 
       {this._currentTime()} 
      </View> 
     ) 
    } 

    _currentTime() { 
     return (
      <View refreshing> 
       <Text>{VideoScreen._toHHMMSS(this.props.currentTime)} 
       </Text> 
      </View> 
     ); 
    } 
} 
+0

Hello Giorgos, 답장을 보내 주셔서 감사합니다. 이것은 내가 생각한 것입니다. 그러나 불행히도 변수를 바인딩 할 수 없습니다. 최소한 bind() 메서드를 사용하여 제안한 것처럼 불가능합니다 :-( –

+0

초를 기다리십시오. 따라서 변수를 함수 ... 나는 그것을 시도하고 작동하는 것을 볼 것입니다. 그동안 고마워요! –

+0

그것은 매력처럼 고마워요! 유일한 것은 당신이 {this._currentTime()} 함수를 호출하여 함수를 실행해야한다는 것입니다. render method ;-) –

관련 문제