Skip to main content

Choosing and sharing state

Many React bugs are not caused by the wrong Hook. They are caused by the wrong state model.

A strong state model answers three questions:

Three state-architecture questions
What truly changes?
Separate real state from values you can derive.
Who owns it?
Choose one authoritative owner for each fact.
Who needs it?
Place state only as high as its consumers require.

Store the minimum state​

Suppose a search screen has products and a query.

const [products, setProducts] = useState(initialProducts);
const [query, setQuery] = useState('');

The filtered list can be calculated:

const visibleProducts = products.filter(product =>
product.name.toLowerCase().includes(query.toLowerCase()),
);

Do not store visibleProducts as separate state unless it represents independent information.

Avoid redundant state​

Bad:

const [firstName, setFirstName] = useState('Ada');
const [lastName, setLastName] = useState('Lovelace');
const [fullName, setFullName] = useState('Ada Lovelace');

Better:

const fullName = `${firstName} ${lastName}`;

Every duplicated representation creates synchronization work.

Avoid contradictory state​

Bad:

const [isLoading, setIsLoading] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);
const [hasError, setHasError] = useState(false);

Impossible combinations can appear:

Contradictory booleans allow impossible UI
isLoading = true
isSuccess = true
hasError = true

A status model may be clearer:

const [status, setStatus] = useState('idle');

with states such as:

One status can model valid workflow states
1idle
2submitting
3success / error

Good reason to group:

const [position, setPosition] = useState({x: 0, y: 0});

x and y describe one coordinate and often update together.

Weak reason to group:

const [state, setState] = useState({
modalOpen: false,
email: '',
selectedTab: 'billing',
notifications: [],
});

These fields may change for unrelated reasons.

Avoid duplicating the same object in state​

Bad:

const [items, setItems] = useState(initialItems);
const [selectedItem, setSelectedItem] = useState(initialItems[0]);

If the item is edited in items, selectedItem can become stale.

Often store identity instead:

const [selectedId, setSelectedId] = useState(initialItems[0].id);

const selectedItem = items.find(item => item.id === selectedId);

State ownership​

Each piece of state should have a clear owner.

Lift state to the closest common owner
owns selectedVariantProductPage
Gallery
reads selected variant
ProductOptions
reads + requests changes

If both children need the selected variant, neither child is a good owner.

Move the state to their closest common parent:

function ProductPage() {
const [selectedVariant, setSelectedVariant] = useState('black');

return (
<>
<Gallery variant={selectedVariant} />
<ProductOptions
value={selectedVariant}
onChange={setSelectedVariant}
/>
</>
);
}

This is lifting state up.

Single source of truth​

“Single source of truth” does not mean all application state belongs in one global object.

It means:

For each unique piece of information, choose one owner.

One owner per fact
Local modal state
Modal feature
Selected product
Product page
Current user
Broader app/session boundary
Server cache
Server-state layer

State should live as low as possible while still correctly serving all consumers.

Controlled components​

A controlled component receives important state through props.

function Panel({isOpen, onOpen}) {
return isOpen
? <p>Details</p>
: <button onClick={onOpen}>Show</button>;
}

The parent owns the behavior:

function Accordion() {
const [activeIndex, setActiveIndex] = useState(0);

return (
<>
<Panel
isOpen={activeIndex === 0}
onOpen={() => setActiveIndex(0)}
/>
<Panel
isOpen={activeIndex === 1}
onOpen={() => setActiveIndex(1)}
/>
</>
);
}

Uncontrolled components​

A component is often called uncontrolled when it owns the relevant state itself.

function Disclosure() {
const [open, setOpen] = useState(false);

return (
<button onClick={() => setOpen(value => !value)}>
{open ? 'Hide' : 'Show'}
</button>
);
}

Neither pattern is universally better.

Controlled vs uncontrolled
SELF-OWNEDUncontrolled

Easier to drop in; harder for a parent to coordinate externally.

PARENT-OWNEDControlled

More configurable and coordinated; requires explicit parent wiring.

State locality before global state​

Before adding Context, Redux, Zustand, or another store, ask whether state can remain local.

How far does this state need to travel?
One componentKeep it local
Siblings must coordinateLift to their closest common parent
Distant consumers / external access / specialised lifecycleConsider composition, Context, or a store based on requirements

Global state is not automatically “more scalable.” It can make ownership harder to reason about.

Do not mirror props into state by default​

Bad:

function Profile({user}) {
const [name, setName] = useState(user.name);
}

Now user.name and name are separate sources of truth.

This can be correct if name intentionally represents an editable draft. If so, name the concept clearly and decide how it resets when a different user arrives.

State shape should model valid UI​

Instead of many loosely related booleans:

const [isEditing, setIsEditing] = useState(false);
const [isSaving, setIsSaving] = useState(false);
const [saved, setSaved] = useState(false);

consider a state that expresses the allowed workflow:

const [status, setStatus] = useState('viewing');

Possible states:

Workflow state should model valid phases
1viewing
2editing
3saving
4saved / error

For more complex transitions, this thinking naturally leads toward reducers or state machines.

Common mistakes​

Storing derived values​

Calculate them during render when possible.

Duplicating shared state in siblings​

Lift it to their closest common owner.

Putting all state at the application root​

State too high causes unnecessary coupling and can widen rendering scope.

Installing a state library too early​

Libraries solve specific coordination and architecture problems. They do not replace understanding ownership.

Debugging state architecture​

When values disagree, ask:

  1. Are two pieces of state representing the same fact?
  2. Can one be derived from the other?
  3. Do multiple components think they own the same value?
  4. Is the owner too high or too low?
  5. Would a reducer make valid transitions clearer?

Production example: filters​

function ProductSearch({products}) {
const [query, setQuery] = useState('');
const [category, setCategory] = useState('all');

const visibleProducts = products.filter(product => {
const matchesQuery = product.name
.toLowerCase()
.includes(query.toLowerCase());
const matchesCategory =
category === 'all' || product.category === category;

return matchesQuery && matchesCategory;
});

return (
<>
<Filters
query={query}
category={category}
onQueryChange={setQuery}
onCategoryChange={setCategory}
/>
<ProductGrid products={visibleProducts} />
</>
);
}

The filtered result is derived. The owner coordinates both the controls and the list.

Exercise​

Design state for a checkout screen containing:

  • delivery method;
  • basket items;
  • basket total;
  • promo code;
  • discount amount;
  • checkout status;
  • modal visibility.

For each value, decide whether it is:

  • real state;
  • derived data;
  • server data;
  • local UI state.

Interview questions​

Junior: What does “lifting state up” mean?

Mid-level: Why is duplicated or contradictory state dangerous?

Senior: How do you decide between local state, Context, an external client store, and server-state tooling?

Summary​

Good state architecture
1Store the minimum
2Avoid contradictions
3Avoid duplication
4Choose one owner per fact
5Lift only as high as necessary

Good state architecture removes bugs before they need debugging.

References​

Next​

Continue with Preserving and Resetting State.