Looks like hoisting is working in React Native with const

Viewed 964

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?

1 Answers

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.

Related