1

I got the error while evaluating a const object, here's a minimal working example:

import React from "react";
import { View, StyleSheet } from "react-native";

const App = () => {
  return (
    <View style={styles.container}></View>
  );
}

const styles = StyleSheet.create({
  container: {
    backgroundColor:colors.bg,
  },
});

const colors = {
  bg:'blue',
}

export default App;

Error message: TypeError: undefined is not an object (evaluating 'colors.bg')

What's going on here?

1 Answer 1

2

You have to define colors const before the styles.

Try This

import React from 'react';
import {View, StyleSheet} from 'react-native';

const App = () => {
  return <View style={styles.container}></View>;
};

const colors = {
  bg: 'blue',
};

const styles = StyleSheet.create({
  container: {
    backgroundColor: colors.bg,
  },
});

export default App;
Sign up to request clarification or add additional context in comments.

2 Comments

This works, thank you. Why colors has to be defined before styles, but styles can be defined after App?
In javascript code will be read line by line that's why in your code colors.bg is not define before calling it thats why its value is undefined. You can upvote if my answer is helpful. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.