Course contentsShow
← React Native
Lesson 288 of 1,5554. Flexbox and Native StylingFree lesson

The Cost of Style Objects

Understand the memory footprint of defining styles inside the render loop versus external static constants.

In this lesson, you will learn how defining style objects directly inside your component’s render loop impacts your application’s memory footprint and performance.

The Hidden Performance Tax

When you define a style object directly inside a component function, like this:

const MyComponent = ({ color }) => {
  return (
    <View style={{ backgroundColor: color, padding: 10, borderRadius: 5 }} />
  );
};

React Native creates a new object in memory every single time MyComponent re-renders. Even if the values inside that object haven't changed, the JavaScript engine sees a fresh memory reference.

Why This Matters

In a small app, this cost is negligible. However, in a complex list (like a FlatList with hundreds of items) or a screen that re-renders frequently, this behavior creates two problems:

  1. Garbage Collection Pressure: The JavaScript engine must constantly clean up these discarded objects, which can cause frame stutters or "jank."
  2. Breakage of Referential Equality: If you pass these inline objects as props to child components that use React.memo, those children will think the props have changed (because the object reference is new) and will re-render unnecessarily, defeating the purpose of your memoization.

The Solution: Static Constants

To avoid this, move your static styles outside of the component body. By defining them as a constant, the object is created once when the module loads, and that same reference is used for the entire lifecycle of the application.

const styles = StyleSheet.create({
  container: {
    padding: 10,
    borderRadius: 5,
  },
});

const MyComponent = ({ color }) => (
  <View style={[styles.container, { backgroundColor: color }]} />
);

In this pattern, the static layout logic (padding, borderRadius) is held in a single memory reference. We only use an inline object for the dynamic part (color) that strictly requires calculation based on component state.

Key Takeaway: Moving static styles outside your component prevents unnecessary object creation on every re-render, reducing memory pressure and keeping your animations buttery smooth.