Skip to content
tezvyn:
Menu

Setting screen options statically versus dynamically

Source: interviewMediumHow cards are made

Summary

static versus dynamic screen options.

Key points

options prop on Screen (can read route) versus navigation.setOptions inside the screen for state-driven changes.

Watch out for

trying to set state-based titles via the static options object only.

What's really being asked

Whether you understand the lifecycle of screen options. Some options are known when you register the screen; others depend on state that only exists after the component renders. The interviewer wants the right tool for each.

The full answer

The first way is the options prop on the Screen element. It can be a plain object or a function that receives route and navigation, so it can read route.params at registration time, for example setting the title from a passed userName. The second way is navigation.setOptions, called from inside the screen component, usually in useLayoutEffect with the relevant dependencies. This updates options on the fly. For a title that derives only from params, the options function is clean. For a title that follows component state, such as an editable document name held in useState, you must use setOptions because the static object cannot see later state.

The mistakes people make

Claiming the static options object can read component state, which it cannot, since it evaluates outside the component. Using useEffect instead of useLayoutEffect, causing a visible flash of the old title. Forgetting the dependency array on setOptions, leading to stale titles or update loops. Mutating the navigation options object directly.

What usually comes next

Why useLayoutEffect over useEffect for header updates? How do you put an interactive component, like a save button wired to local state, into the header? How do screenOptions on the navigator differ from per-screen options?

A concrete example

In an EditNote screen the title should mirror the text being typed. You keep title in useState, then call useLayoutEffect with a dependency on title that runs navigation.setOptions with headerTitle equal to title or a placeholder. As the user types, the header updates live. By contrast, a read-only Profile screen sets its title once via options: ({ route }) => ({ title: route.params.name }).

Interview question

You need a header title that updates live as the user edits a text field held in component state. What should you use?

  • a.The static options object on the Screen, reading the state value
  • b.navigation.setOptions inside useLayoutEffect with the state as a dependencyCorrect
  • c.The route.params object, mutating it on each keystroke
  • d.screenOptions on the navigator with the state passed in
Why?

The static options object evaluates outside the component and cannot see evolving state. setOptions in useLayoutEffect updates the header from current state without a visible flash.

Just read this? Test yourself on what you have been reading.

Read the original → reactnavigation.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react-native — each one lists the topics its interview covers.

See open roles