[英]Looks like hoisting is working in React Native with const
I noticed one interesting thing in react native. 我注意到一个有趣的事情在本地反应。 I thought that const and let does not support hoisting in ES6. 我认为const和let不支持在ES6中提升。 How is it possible to use const styles above its define? 如何在其定义之上使用const 样式 ?
render() {
const { repos } = this.state;
const reposList = repos.map((rep, index) => {
return (
<Text>{rep.name}</Text>
)
});
return (
<View style={styles.container}> <-- styles should not be defined
{reposList}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Is it because of hoisting mechanism in React Native? 是因为React Native中的提升机制吗?
you have render defined in a class, the class is just defined not executed so it can see the styles you create below it. 如果在类中定义了渲染,则只定义该类未执行,以便它可以看到您在其下创建的样式。 it's not really hoisting. 它并没有真正起吊。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.