Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add defaultValue prop to search bar #2558

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rrebase
Copy link

@rrebase rrebase commented Dec 8, 2024

Description

Add defaultValue prop to search bar, enabling initial text value configuration.

useLayoutEffect(() => {
  navigation.setOptions({
    headerSearchBarOptions: {
      defaultValue: 'Some default value',
    },
  });
}, [navigation]);

A common use-case for a default value is deep linking to a search screen that makes use of the search bar.

It's possible to imperatively set the value via ref using setText, however there's no the suitable callback to do it reliably without seeing the initial empty value / flickering.

Named the prop defaultValue to be consistent with RN TextInput where it's also used for the initial value for uncontrolled TextInput component.

While it works well for my use case, I'm not fully sure if adding a defaultValue is the best approach with the imperative nature / paradigm how react-native-screens defines the search bar, maybe there's obvious downsides that I'm missing, would love to hear your thoughts.

Test code and steps to reproduce

Tested in my app with a patch with these changes on:

  • iOS (paper)
  • Android (paper)

I tried to run the react-native-screens example app, but got 'No component found for view with name "RNScreenStackHeaderConfig" on iOS after following the Working on iOS steps and didn't have time to debug it atm. Also the steps mention yarn prepare in react-navigation, but there's no such script.

Checklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expo - Unable to set default text for header search bar using ref
1 participant