我有一些 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!
为什么我在 constructor
或 componentWillMount
中看不到日志记录,它们应该在 render
方法之前调用?
编辑:
对于日志记录,我使用的是 react-native log-android
。
最佳答案
看起来问题可能出在 react-native log-android
我刚刚在 Google Chrome 上测试了您的代码,似乎运行良好。
也许这将有助于查明问题是否出在 react-native log-android 上? How to make console.log work in react native for android
关于javascript - 不调用构造函数,但调用渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852481/