不调用构造函数,但调用render

时间:2017-10-20 15:32:08

标签: javascript reactjs react-native

我有一些React Native代码,其中我有以下React类:

import React, { Component } from 'react';
import { ScrollView, StyleSheet, TouchableHighlight, View, Text,  Platform } from 'react-native';
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux';

export default class MyList extends Component {
  constructor(props) {
      super(props);
      console.log("In constructor!");
      //some other code
  }

  componentWillMount() {
    console.log("In componentWillMount!");
  }

  render() {
    console.log("In render method!");
        return ( 
        <ScrollView
          horizontal={true} 
          showsHorizontalScrollIndicator={true}
          onLayout={this._onScrollViewLayout}
          onScroll={this._onScroll}
          ref={SCROLLVIEW_REF}
          scrollEventThrottle={8}
        > 
         //some other code
         </ScrollView>

        );
  }
}

问题是只有这个打印在日志中:

 ReactNativeJS: In render method!

为什么我无法在constructorcomponentWillMount中看到记录,应该在render方法之前调用?

修改

对于日志记录,我使用react-native log-android

1 个答案:

答案 0 :(得分:1)

问题可能与react-native log-android

有关

我刚刚在Google Chrome上测试了您的代码,这似乎工作正常。

也许这有助于确定问题是否与react-native log-android有关? How to make console.log work in react native for android