我正在学习react native
.对于下面的代码:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
export default class App extends Component {
highlight() {
console.log(111111111);
}
render() {
console.log(222222);
console.log(this.highlight());
console.log(333333);
return (
<View>
<Text>sdsd</Text>
</View>
);
}
}
我在控制台上得到这个输出:
2222222
111111111
undefined
33333333
并在设备屏幕上sdsd
。 那undefined
干什么?? 如果是undefined
它如何在函数内运行日志?
我认为在 render() 中您想记录 highlight() 返回的内容。但是突出显示不会返回它自己记录的任何内容。
所以渲染应该看起来像这样:
render() {
console.log(222222);
this.highlight();
console.log(333333);
return (
<View>
<Text>sdsd</Text>
</View>
);
}
console.log(this.highlight());
打印出this.highlight()
的返回,但this.highlight()
不返回任何内容。
要么从this.highlight()
返回值并在调用站点打印,要么只写this.highlight()
.