Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React native. How to pass state from component to BottomTab.Screen?

I got navigation made with "bottom-tab navigation" in react native. Example of code

<BottomTab.Screen
    name="Home"
    component={RootPasswordsHandler}
    options={(): {
      tabBarIcon: ({ color }: { color: any }) => JSX.Element;
      headerRight: () => JSX.Element;
      title: string;
    } => ({
      title: globalState.languageHandler.screenNames.HOME_SCREEN,
      tabBarIcon: ({ color }): JSX.Element => <TabBarIcon name="home" color={color} />,
      headerRight: (): JSX.Element => (
        <TextInput style={styles.navStyles.input} placeholder={globalState.languageHandler.generic.FIND_PASSWORD} />
      ),
    })}
  />

In component "RootPasswordsHandler", component fetch passwords from database, save them in local state ( useState ) and render them. I would like to add input ( as in code example above ), which will access that data and filter array of passwords. How can I achieve that ? I am pretty new to react native and I am completely stuck here :/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use setOptions in your component and then access them in BottomTab

Example from docs see how value is passed!

function ProfileScreen({ navigation, route }) {
  const [value, onChangeText] = React.useState(route.params.title);

  React.useLayoutEffect(() => {
    navigation.setOptions({
      title: value === '' ? 'No title' : value,
    });
  }, [navigation, value]);

  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <TextInput
        style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
        onChangeText={onChangeText}
        value={value}
      />
      <Button title="Go back" onPress={() => navigation.goBack()} />
    </View>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading