简体   繁体   中英

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. How is it possible to use const styles above its define?

    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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM