这是我的代码的一部分:
render() {
console.log("first log");
return (
<View onLayout={e => console.log("second log")}>
...
Run Code Online (Sandbox Code Playgroud)
控制台显示“第一个日志”,但不显示第二个。视图似乎正确渲染。我尝试了其他观点,但结果是一样的。我正在使用本机0.48.4。
我通过将初始高度更改为大于0的方式解决了此问题,然后在更改ChildView的布局后成功触发了onLayout函数。
constructor(props) {
super(props);
this.state = {
title: '',
height: 0 // changed to 1
};
}
onLayout = (event) => {
console.log('on layout:')
const {
x,
y,
width,
height
} = event.nativeEvent.layout;
console.log(x, y, width, height);
}
render() {
return (
<View style={[styles.container, {height: this.state.height}]}>
<View onLayout={this.onLayout}>
<ChildView/>
</View>
</View>
)
}Run Code Online (Sandbox Code Playgroud)
由于此重大 更改: Android:仅当布局实际更改时才调用onLayout https://github.com/facebook/react-native/wiki/Breaking-Changes#android-only-call-onlayout-when-layout-has-实际上改变了15429e --- astreet
| 归档时间: |
|
| 查看次数: |
5180 次 |
| 最近记录: |