react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

Search bar not rendering on iOS or Android on initial render

Open homit-dalia opened this issue 1 year ago โ€ข 25 comments

Current behavior

In the initial render of the search bar through headerSearchBarOptions, it will not render. I have 4 bottom tabs and each has its own stack navigator. Search Icon does not render on initial load but would load after I switch to another tab and then current tab.

Expected behavior

The search icon should render on the first render of the screen

Reproduction

https://github.com/react-navigation/react-navigation/issues/10848

Platform

  • [X] Android
  • [X] iOS
  • [ ] Web
  • [ ] Windows
  • [ ] MacOS

Packages

  • [X] @react-navigation/bottom-tabs
  • [ ] @react-navigation/drawer
  • [ ] @react-navigation/material-top-tabs
  • [ ] @react-navigation/stack
  • [X] @react-navigation/native-stack
  • [ ] react-native-tab-view

Environment

  • [] I've removed the packages that I don't use
package version
@react-navigation/native
@react-navigation/bottom-tabs
@react-navigation/drawer
@react-navigation/material-top-tabs
@react-navigation/stack
@react-navigation/native-stack
react-native-safe-area-context
react-native-screens
react-native-gesture-handler
react-native-reanimated
react-native-tab-view
react-native-pager-view
react-native
node
npm or yarn

homit-dalia avatar Oct 06 '23 19:10 homit-dalia

Hey @homit-dalia! Thanks for opening the issue. It seems that the issue doesn't contain a link to a repro.

The best way to get attention to your issue is to provide an easy way for a developer to reproduce the issue.

You can provide a repro using any of the following:

A snack link is preferred since it's the easiest way to both create and share a repro. If it's not possible to create a repro using a snack, link to a GitHub repo under your username is a good alternative. Don't link to a branch or specific file etc. as it won't be detected.

Try to keep the repro as small as possible by narrowing down the minimal amount of code needed to reproduce the issue. Don't link to your entire project or a project containing code unrelated to the issue. See "How to create a Minimal, Reproducible Example" for more information.

You can edit your original issue to include a link to the repro, or leave it as a comment. The issue will be closed automatically after a while if you don't provide a repro.

github-actions[bot] avatar Oct 06 '23 19:10 github-actions[bot]

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/native
  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • react-native-tab-view

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

github-actions[bot] avatar Oct 06 '23 19:10 github-actions[bot]

Any update on this?

Acetyld avatar Oct 13 '23 18:10 Acetyld

@Acetyld I initially set headerSearchBarOptions to true as the documentation stated that you should render atleast the UI elements in stack screens if you will update it on screen mount through .setOptions in useEffect (functional components). That was the problem. I removed headerSearchBarOptions in stack screen entirely and and now declaring it directly through .setOptions. I don't know if this is expected or headerSearchBarOptions : true is for a different context.

homit-dalia avatar Oct 14 '23 06:10 homit-dalia

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/native
  • @react-navigation/bottom-tabs
  • react-native-tab-view
  • @react-navigation/drawer
  • @react-navigation/material-top-tabs
  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

github-actions[bot] avatar Oct 24 '23 14:10 github-actions[bot]

Any updates or workarounds? :/ Using setOptions does not work on Android. iOS works fine without any hacks.

Robert27 avatar Nov 08 '23 00:11 Robert27

Any update on this?

Iโ€™m sorry @Robert27, but it fixed it for me after I removed headerSearchBarOptions: true in declaration and rebuilt the app.

However, it could also be an issue with if you are rendering other icons in headerRight. Try playing around the placement of those icons and see if it works.

homit-dalia avatar Nov 08 '23 04:11 homit-dalia

Still not working for me with the latest versions. What about you guys?

JcbPrn avatar Nov 15 '23 19:11 JcbPrn

Hey guys, I think I fixed it by setting hideWhenScrolling: false:

  const navigation = useNavigation<Screen>()
  useLayoutEffect(() => {
    navigation.setOptions({
      headerSearchBarOptions: {
        placeholder: 'Search...',
        onCancelButtonPress: actions.stopSearching,
        onFocus: actions.startSearching,
        onChangeText: (e) => actions.onChangeText(e.nativeEvent.text),
        hideWhenScrolling: false,
      },
    })
  }, [actions, navigation])

Let me know if this fixes it for you.

Be sure to also have your scrollable component as the root node of your screen (not wrapped in a View), and to set contentInsetAdjustmentBehavior='automatic'. It also broke if I used refreshControl.

nandorojo avatar Nov 16 '23 23:11 nandorojo

@nandorojo I have still the issue on Android, but works on ios.

vbornand avatar Nov 17 '23 09:11 vbornand

I have this problem on Android only. Adding the hideWhenScrolling: false property does not solve the problem

    "@react-navigation/native": "^6.0.8",
    "@react-navigation/native-stack": "^6.2.5",
    "@react-navigation/stack": "^6.0.11",

WhidRubeld avatar Nov 17 '23 16:11 WhidRubeld

Hello ๐Ÿ‘‹, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

github-actions[bot] avatar Dec 18 '23 01:12 github-actions[bot]

I have this problem on Android only. Adding the hideWhenScrolling: false property does not solve the problem

    "@react-navigation/native": "^6.0.8",
    "@react-navigation/native-stack": "^6.2.5",
    "@react-navigation/stack": "^6.0.11",

Hi, I have the same problem. Did you solve the problem? ๐Ÿ™๐Ÿป

ysnbyzli avatar Dec 22 '23 09:12 ysnbyzli

I have this problem on Android only. Adding the hideWhenScrolling: false property does not solve the problem

    "@react-navigation/native": "^6.0.8",
    "@react-navigation/native-stack": "^6.2.5",
    "@react-navigation/stack": "^6.0.11",

Hi, I have the same problem. Did you solve the problem? ๐Ÿ™๐Ÿป

No, I haven't found a way around this problem. So I temporarily worked around a search modal for Android...

WhidRubeld avatar Dec 22 '23 10:12 WhidRubeld

With expo-router 3.4.1 using: @react-navigation/native: 6.1.9 @react-navigation/native-stack: 6.9.17

The issue is resolved

vbornand avatar Dec 22 '23 10:12 vbornand

It turns out there was an issue with react-native-screens here. Updating to 3.26.0 fixed it for me.

sidorchukandrew avatar Dec 23 '23 03:12 sidorchukandrew

It turns out there was an issue with react-native-screens here. Updating to 3.26.0 fixed it for me.

@sidorchukandrew Hi ๐Ÿ‘‹๐Ÿป, I using react-native-navigation on my expo project. When use headerSearchBarOptions not showing search icon on screen header. As you said, when I change the react-native-screens version to 3.26.0, the application stays in the splash screen, which means it crashes. So I'm using the latest version but again the search function still doesn't work.

Thank you for your interest ๐Ÿ™๐Ÿป

"dependencies": {
    "@expo/vector-icons": "^13.0.0",
    "@react-native-async-storage/async-storage": "1.18.2",
    "@react-navigation/native": "^6.1.9",
    "@react-navigation/native-stack": "^6.9.17",
    "@reduxjs/toolkit": "^2.0.1",
    "@shopify/flash-list": "1.4.3",
    "expo": "~49.0.15",
    "expo-font": "~11.4.0",
    "expo-splash-screen": "~0.20.5",
    "expo-sqlite": "~11.3.3",
    "expo-status-bar": "~1.6.0",
    "react": "18.2.0",
    "react-native": "0.72.6",
    "react-native-gesture-handler": "~2.12.0",
    "react-native-reanimated": "~3.3.0",
    "react-native-safe-area-context": "4.6.3",
    "react-native-screens": "^3.29.0",
    "react-redux": "^9.0.4"
  }
useLayoutEffect(() => {
    navigation.setOptions({
      headerLargeTitle: true,
      headerSearchBarOptions: {
        inputType: "text",
        cancelButtonText: "ฤฐptal",
        tintColor: colors.primary,
        placeholder: "รœrรผn ara",
        onChangeText: (event) => {
          setSearch(event.nativeEvent.text);
        },
        onFocus: () => {
          setIsSearchFocused(true);
        },
        onBlur: () => {
          setIsSearchFocused(false);
        },
        hideWhenScrolling: false
      }
    });
  }, [navigation, colors]);

ysnbyzli avatar Dec 23 '23 08:12 ysnbyzli

With expo-router 3.4.1 using: @react-navigation/native: 6.1.9 @react-navigation/native-stack: 6.9.17

The issue is resolved

For me itโ€™s not, I have the same versions. While Android is fixed, it started to appear on iOS. Now search bar is not rendered on initial render somehow.

Robert27 avatar Dec 25 '23 13:12 Robert27

With expo-router 3.4.1 using: @react-navigation/native: 6.1.9 @react-navigation/native-stack: 6.9.17

The issue is resolved

I doubt this is true. Because the problem lies in the native layer. Expo system relies on stable versions of libraries (mainly major outputs) from version to version of Expo SDK. Therefore, I think that it is worth waiting for the update along with the Expo SDK update to version 50. There will most likely already be support for React Navigation 7.

For now, I see as an alternative option to write a plugin for Expo, which will improve the work of the package.

WhidRubeld avatar Dec 26 '23 17:12 WhidRubeld

Therefore, I think that it is worth waiting for the update along with the Expo SDK update to version 50

It is resolved with Expo 50 still in preview but should be released in a couple of weeks. As mentioned above the bug is in another library.

vbornand avatar Dec 26 '23 18:12 vbornand

No itโ€™s definitely not resolved in expo sdk 50 using the latest versions of the libs. We have a stack view with searchbar inside my tab bar and it is not rendering on the initial render on iOS. Without being nested in a tab bar it works fine. But I agree with you that the bug is in another library.

Robert27 avatar Dec 26 '23 18:12 Robert27

Therefore, I think that it is worth waiting for the update along with the Expo SDK update to version 50

It is resolved with Expo 50 still in preview but should be released in a couple of weeks. As mentioned above the bug is in another library.

Is there a link to MR in react-native-screens to make sure that Expo 50 will merge the solution to the problem?

WhidRubeld avatar Dec 26 '23 20:12 WhidRubeld

https://github.com/software-mansion/react-native-screens/pull/1883

WhidRubeld avatar Dec 26 '23 20:12 WhidRubeld

No itโ€™s definitely not resolved in expo sdk 50 using the latest versions of the libs. We have a stack view with searchbar inside my tab bar and it is not rendering on the initial render on iOS. Without being nested in a tab bar it works fine. But I agree with you that the bug is in another library.

Update: When just setting the searchbar dynamically using navigation.setOptions the described bug still occurs inside the tabbar, but when providing a static default searchbar using the headerSearchBarOptions in Stack.Screen it always works fine on iOS.

Robert27 avatar Jan 09 '24 08:01 Robert27

Still issue on Android with Expo 50

amitava82 avatar Feb 05 '24 10:02 amitava82

Hello ๐Ÿ‘‹, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

github-actions[bot] avatar Mar 07 '24 01:03 github-actions[bot]

Lets keep it open, still present

Acetyld avatar Mar 07 '24 18:03 Acetyld

Not sure if this helps to debug, but maybe someone find useful as workaround.

For me issue fixes itself if I remove refreshing and onRefresh prop from my FlatList. My code looks more or less like this

  useLayoutEffect(() => {
    props.navigation.setOptions({
      headerSearchBarOptions: {
        hideWhenScrolling: false,
        inputType: "text",
        placeholder: "Search assets",
        onChangeText: (event) => {
          setSearchParams((params) => ({
            ...params,
            search: event.nativeEvent.text,
          }));
        },
      },
    });
  }, [props.navigation]);

Component:

 return (
    <FlatList
      // refreshing={loading}
      // onRefresh={() => loadAssets(searchParams)}
      contentInsetAdjustmentBehavior="automatic"
      data={list}
        ListFooterComponent={() => {
        if (!loading) return null;
        return (
          <View style={styles.loadingIndicator}>
            <ActivityIndicator size="large" />
          </View>
        );
      }
      // Rest of component ...
      }} 
     />

Here are my dependencies:

    "@react-navigation/native": "^6.1.15",
    "@react-navigation/native-stack": "^6.9.24",
    "expo": "~50.0.11",
    "expo-status-bar": "~1.11.1",
    "react": "18.2.0",
    "react-native": "0.73.4",
    "react-native-safe-area-context": "4.8.2",
    "react-native-screens": "~3.29.0"

Here are screen recordings 1) Without passing refreshing and onRefresh, 2) With refreshing and onRefresh I am guessing it is something with calculating changes on layout?

https://github.com/react-navigation/react-navigation/assets/19210348/3ab9bd7a-8eaf-436e-884e-fccccbfe4545 https://github.com/react-navigation/react-navigation/assets/19210348/19c3e364-d3b7-4d6f-ad4d-9ef9709ff5a4

Let me know if there is anything I can provide to help it debug

Faliszek avatar Mar 13 '24 09:03 Faliszek

It didn't make any difference. The basic repo case in issue https://github.com/react-navigation/react-navigation/issues/10956 has no FlatList

amitava82 avatar Mar 13 '24 10:03 amitava82

For me, if I remove FlatList then the search bar will render initially; however, if I add the FlatList I have to scroll first for it to appear.

coofzilla avatar Mar 22 '24 02:03 coofzilla