Skip to the workspace
Pebellslearn by building
Environment Setup  ›  Capstone Project

Props and State on a Phone

State 14 minProps are read-only inputs passed from a parent, while state is data a component owns and changes with a setter, which triggers a re-render.

react native / Props and State on a Phone

Explore each scene at your own pace.

01 · The rule

The rule

Props and State on a Phone

A plant card in Sprout must show a different plant each time, and a "Watered" count must go up when you tap. Those are two different kinds of data. React calls them props and state, and they work in React Native exactly as they do on the web.

The rule

Props come in from the parent; state lives inside and changes.

Props are the inputs a parent passes to a child. The child reads them but never changes them:

function PlantCard({ name, everyDays }) {
  return (
    <View>
      <Text>{name}</Text>
      <Text>Water every {everyDays} days</Text>
    </View>
  );
}

<PlantCard name="Fern" everyDays={3} />

State is data a component owns and can change over time. You create it with the useState hook. Calling the setter makes React render the component again with the new value:

import { useState } from 'react';
import { View, Text, Button } from 'react-native';

function WaterCounter() {
  const [times, setTimes] = useState(0);
  return (
    <View>
      <Text>Watered {times} times</Text>
      <Button title="Watered" onPress={() => setTimes(times + 1)} />
    </View>
  );
}

Two React Native details: event props are named for touches (onPress, not onClick), and a Button takes its label as the title prop rather than as children.

Props can also be functions. The list can pass onWatered={() => markWatered(plant.id)} to each card, and the card calls it from its button. That is how a child reports a tap back up to the parent that owns the data.

When a parent owns state and passes it down as a prop, the child always shows the latest value. This "state up, props down" flow is how the whole Sprout app stays consistent.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.