Skip to main content

Understanding Props and State in React Native

Your goal: Make each BookCard in your Reading List interactive while keeping its title and author supplied by the screen.

Before you begin: Complete Components and JSX. Open the same learning project and run npx expo start from its folder.

By the end, you will be able to:

  • Pass and read typed props.
  • Explain how props differ from state.
  • Use useState and a press handler to toggle one card’s read status.

Give BookCard its inputs​

Here is the BookCard from the previous lesson, with styling left out so we can focus on its inputs:

Reading the card’s props
import { Text, View } from 'react-native';

type BookCardProps = {
title: string;
author: string;
};

function BookCard({ title, author }: BookCardProps) {
return (
<View>
<Text>{title}</Text>
<Text>{author}</Text>
</View>
);
}

Props are inputs passed from a parent to a child. Inside ReadingListScreen, the parent supplies those inputs as JSX attributes:

<BookCard title="The Hobbit" author="J. R. R. Tolkien" />
<BookCard title="A Wizard of Earthsea" author="Ursula K. Le Guin" />

The same component displays different values for each use. BookCardProps tells TypeScript that title and author are required strings. A missing author or a number supplied as a title produces a type error in your editor.

The parameter { title, author } destructures the props object: it takes those named properties so the function can use them directly. Without destructuring, the parameter could be props: BookCardProps, and you would read props.title and props.author.

Props are read-only from the receiving component’s perspective. BookCard should not change its props. The parent can supply new values later; read-only does not mean props can never change. See React’s guide to passing props.

Remember a value with state​

State is a component’s memory for data that changes during interaction. The title comes from the parent, but each card will remember whether you have marked it as read.

Import useState from React, then call it at the top level of BookCard, before its return. These are fragments to read; the complete screen below puts them together.

Import at the top of the file
import { useState } from 'react';
Inside BookCard, before return
const [isRead, setIsRead] = useState(false);

useState is a React Hook that gives the component a state value and a setter. Here, false is the initial value, so every new card starts unread. The square-bracket syntax names the two returned values: isRead holds the current boolean (true or false), and setIsRead requests an update. TypeScript infers the boolean type from false.

When the setter changes the value, React renders the component again using the new state. React remembers that state between renders; it does not reset to false on every render. Each use of BookCard has independent state. A full app reload starts these cards unread again; this example does not save progress.

PropsState
Where does it come from?The parent supplies it.The component initializes it.
How does it change?The parent passes new inputs.The component calls its setter.
In our cardtitle and authorisRead

Update state through its setter​

This is incorrect:

Do not assign to state
isRead = true;

Our isRead binding is declared with const, so assignment is an error. Changing it to let would not solve the React problem: ordinary variable assignment does not tell React to render with new state.

Use the setter in a press handler instead:

Set a known value
setIsRead(true);

That marks the card as read regardless of its previous value. To toggle, the previous value matters:

Calculate from the previous value
setIsRead(previous => !previous);

React passes the pending state to this updater function. ! means “not”: it turns false into true and true into false. Use this functional form when the next value depends on the previous one, especially when updates may be queued together. The setter schedules the next render; it does not change the isRead value in a handler that is already running. See the useState reference.

Run the interactive Reading List​

Replace the contents of src/app/index.tsx, the Expo Router screen established in Development Environment, with this complete example. If your template used a different screen file, replace the same file you edited in the previous lesson. Keep the surrounding layout files. For a traditional project whose entry file is App.tsx, paste this same example there instead; its default export works as the root component too.

src/app/index.tsx — complete example
import { useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';

type BookCardProps = {
title: string;
author: string;
};

function BookCard({ title, author }: BookCardProps) {
const [isRead, setIsRead] = useState(false);
const buttonLabel = isRead ? 'Mark as unread' : 'Mark as read';

return (
<View style={styles.card}>
<Text style={styles.bookTitle}>{title}</Text>
<Text>{author}</Text>
<Text style={styles.status}>
Status: {isRead ? 'Read' : 'Unread'}
</Text>
<Pressable
style={styles.button}
onPress={() => setIsRead(previous => !previous)}
accessibilityRole="button"
accessibilityLabel={buttonLabel + ': ' + title}
>
<Text style={styles.buttonText}>{buttonLabel}</Text>
</Pressable>
</View>
);
}

function ReadingListScreen() {
return (
<View style={styles.screen}>
<Text style={styles.heading}>My Reading List</Text>
<Text style={styles.subtitle}>Three books to explore next</Text>
<BookCard title="The Hobbit" author="J. R. R. Tolkien" />
<BookCard title="A Wizard of Earthsea" author="Ursula K. Le Guin" />
<BookCard title="The Secret Garden" author="Frances Hodgson Burnett" />
</View>
);
}

export default function App() {
return <ReadingListScreen />;
}

const styles = StyleSheet.create({
screen: { padding: 24, paddingTop: 64 },
heading: { fontSize: 28, fontWeight: '700' },
subtitle: { marginBottom: 20 },
card: { padding: 12, marginBottom: 12, borderWidth: 1 },
bookTitle: { fontSize: 18, fontWeight: '600' },
status: { marginTop: 8, marginBottom: 8 },
button: {
minHeight: 44,
padding: 12,
backgroundColor: '#1e40af',
borderRadius: 4,
},
buttonText: { color: '#ffffff', fontWeight: '600' },
});

Save and look at your phone. You should see the same heading and three bordered book cards. Each now shows Status: Unread and a blue Mark as read button. Press the first button: only The Hobbit changes to Status: Read, and its button becomes Mark as unread. Press it again to reverse the change.

The expression isRead ? 'Read' : 'Unread' chooses the first string when isRead is true, and the second when it is false. buttonLabel uses the same conditional syntax and is recalculated during each render; it does not need separate state.

Pressable handles the interaction. onPress receives a function, () => ..., which runs when pressed. That function calls the setter; React renders the card with the new state, updating both the status and label.

The accessibility properties identify the control as a button and name its action and book, such as “Mark as read: The Hobbit.” The label changes with the visible action, so the three buttons can be distinguished with a screen reader.

Like the previous lesson, this is a small, fixed screen with practice top padding, not device-aware spacing or scrolling. Small displays or enlarged text may clip the cards; adapting that layout is outside this lesson.

Fix common mistakes​

If you see…Check this
useState or Pressable cannot be foundImport useState from react and Pressable from react-native.
An assignment error, or no UI updateUse setIsRead(...), not isRead = ....
A render loop before pressing anythingPass onPress={() => setIsRead(true)}, not onPress={setIsRead(true)}. The latter calls the setter while rendering.
A Hook errorCall useState at the top level of BookCard, not inside a condition or press handler.
Every card changes togetherKeep isRead inside BookCard so each instance owns its state.

Practice​

  1. Change an input. Edit the first card’s title prop. Check that its title and accessibility label use the new name while the other cards stay unchanged.
  2. Try a different initial value. Temporarily use useState(true). Fully reload the app to see all three cards start read, then restore false. Fast Refresh may preserve state during edits.
  3. Set a known value. Temporarily change the handler to onPress={() => setIsRead(true)} and the label to “Mark as read.” Repeated presses should leave the card read. Restore the toggle and its changing label.

Check your understanding​

  1. Why are title and author props, while isRead is state?
  2. Why does pressing one card leave the other two unchanged?
  3. How does setIsRead(true) differ from setIsRead(previous => !previous)?
Compare your answers

The screen supplies the book details; each card remembers its own interaction state. Separate card instances have separate state. Passing true sets a known value, while the updater calculates the opposite of the previous value.

You are ready to continue when: You can toggle any card independently and explain how its props, state, and press handler produce the displayed result.

Revisit Components and JSX or return to the learning path.