Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 71 ยท Components and Interaction

Smart and Presentational Components, Importing and Exporting

Split React Native screens into smart (container) and dumb (presentational) components, and learn default and named exports, index files and how Metro resolves imports.

A screen that fetches data, keeps state, handles taps and also draws every pixel becomes hard to read, test and reuse. The oldest cure in React is to split components into two kinds: smart components that know what to do, and dumb (presentational) components that know only how things look. Along the way you need to move components into separate files and import them, which has its own rules in React Native: default and named exports, index files, and the platform-specific file extensions Metro understands.

What runs on this site. The component code is React Native, so it is read-only here. The runnable examples are stand-ins: the same component split written with HTML elements, and a small simulation of how imports are resolved.

Two kinds of components

Dumb (presentational) Smart (container)
Concern how it looks how it works
Data comes from props only state, hooks, APIs, context
Own state none, or purely visual (is a tooltip open?) yes
Side effects none fetching, storage, navigation
Reuse everywhere usually one screen
Test with a few props mocked data sources

The names are historical, and "dumb" is not an insult: a dumb component is the easiest one to understand, because given the same props it always draws the same thing. Modern React code uses hooks, so the "smart" part is often a custom hook (usePosts) or a screen component that calls it, and the dumb part is a function component that receives what it needs.

A dumb component

JSX
// PostCard.js: knows nothing about where posts come from
import { View, Text, Pressable, StyleSheet } from 'react-native';

export function PostCard({ title, author, likes, onLike }) {
  return (
    <View style={styles.card}>
      <Text style={styles.title}>{title}</Text>
      <Text style={styles.meta}>by {author}</Text>
      <Pressable onPress={onLike} accessibilityRole="button">
        <Text>{likes} likes, tap to like</Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  card: { padding: 12, borderRadius: 8, backgroundColor: 'white', gap: 4 },
  title: { fontSize: 18, fontWeight: '600' },
  meta: { color: '#64748b' },
});

Its characteristics: it receives data and callbacks through props; it holds no business state; it does not fetch; it can be drawn in any state by passing different props. It is a pure description of one slice of the UI.

A smart component

JSX
// PostsScreen.js: owns the data and the behavior
import { FlatList, ActivityIndicator, Text } from 'react-native';
import { PostCard } from './PostCard';
import { usePosts } from './usePosts';

export default function PostsScreen() {
  const { posts, status, like } = usePosts();

  if (status === 'loading') return <ActivityIndicator />;
  if (status === 'error') return <Text>Could not load posts.</Text>;

  return (
    <FlatList
      data={posts}
      keyExtractor={(p) => String(p.id)}
      renderItem={({ item }) => (
        <PostCard title={item.title} author={item.author} likes={item.likes} onLike={() => like(item.id)} />
      )}
    />
  );
}

The screen decides which state to show and how a "like" changes the data, and hands the result to PostCard. When the design changes, you edit the dumb component. When the API changes, you edit the hook. Neither edit touches the other.

The same split, as a runnable stand-in

When to split

Do not split by reflex. Split when a component both fetches or mutates and has substantial markup; when you want to show the same card in two places; or when you want to test the appearance without a network. A small screen that fetches one value and shows it in three lines does not need two files. The goal is clarity, not a ceremony.

A related point is the smart/dumb boundary in state: keep state as low as it can go, and lift it only when siblings need it (see State in Native Apps).

Importing components

Every component lives in a file, and you bring it into another file with import. There are two kinds of export.

JSX
// Button.js
export function Button() { /* named export */ }
export function IconButton() { /* a second named export */ }

// Card.js
export default function Card() { /* the file's one default export */ }
JSX
// Importing
import Card from './Card';                    // default: you choose the local name
import { Button, IconButton } from './Button'; // named: braces and exact names
import { Button as PrimaryButton } from './Button'; // rename on import
import * as Controls from './Button';          // everything as an object: <Controls.Button />

Named exports are safer: the name is the same everywhere, and editors can auto-import them. Default exports are common for screens. Mixing them up is the commonest error: importing a named export without braces gives undefined, and React then fails with "Element type is invalid: expected a string... but got: undefined".

Paths and index files

An import beginning with ./ or ../ is relative to the current file. A name with no dot, like react-native, comes from node_modules. When the path names a folder, Metro looks for index.js inside it, which lets you collect a folder's components in one place (a barrel file):

JSX
// components/index.js
export { Button } from './Button';
export { Card } from './Card';

// anywhere else
import { Button, Card } from '../components';

Barrels are neat but make every import load the whole folder, which hurts startup time in large apps. Use them for small groups.

For deep trees, replace ../../../components with an alias (@/components), configured in tsconfig.json (or babel.config.js) and supported by Expo.

Project structure

A common layout:

Text
src/
  components/      dumb, shared pieces (Button, Card, PostCard)
  screens/         smart screens (PostsScreen)
  hooks/           data logic (usePosts)
  services/        API and storage functions
  theme/           colors and spacing

How Metro finds the file

When you write import { Button } from './Button' with no extension, Metro tries a list of candidates in order. It prefers platform-specific files over general ones (see Platform-Specific Code).

Which file does Metro pick? (resolution stand-in)

Switch the platform: Button resolves to Button.ios.js on iOS and plain Button.js on Android, and shadow exists only for Android, so on iOS it fails with the "Unable to resolve module" error you will certainly meet at some point.

Rename or move a file and the app will not rebuild? Clear Metro's cache with npx expo start --clear. Also remember that file names are case-sensitive on most build servers even if your laptop is not, so import './button' may work locally and fail in CI.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which description fits a presentational (dumb) component?

    A presentational component receives data and callbacks through props and focuses on appearance.
  2. 2. You wrote import Button from './Button' but Button.js has export function Button(). What happens?

    A named export needs braces. Without them you ask for the default export, which does not exist.
  3. 3. What does Metro pick for import './Button' on iOS when both Button.js and Button.ios.js exist?

    Platform-specific files take precedence over the generic one.
  4. 4. Where does the data logic of a smart component often live in modern code?

    Hooks let you move fetching and state out of the markup so the screen stays small and the dumb components stay pure.

Summary

  • Smart components own data and behavior; dumb components draw what they receive through props.
  • Move logic into hooks, keep presentational components pure, and split only when it helps clarity or reuse.
  • Named exports need braces; default exports are imported under any name. Mixing them up gives undefined.
  • A folder import resolves to index.js; barrels are handy but load everything, and aliases tame deep paths.
  • Metro prefers .ios.js or .android.js files, and file names are case-sensitive on build servers.

Next: Accessibility in React Native.