45

This is my first post on StackOverflow, so apologies if I'm not following the correct format.

I'm building my first app using tab navigator from React Navigation v 5.x and have run into a big problem when passing props from one screen to another

What I want to achieve is to:

  1. Make changes to a list of data in one of my screens.
  2. Have those changes affect what happens on another screen.

I have tried this (I failed to set the props to pass down), this (Deprecated version of react-navigation) and this (Also old version of react-navigation).

and Redux but there are no examples available with the current version of react navigation.

I'm at the end of my rope with this and really need some step by step on how to achieve this. Here's a rough sketch of what I want to do:

enter image description here

The way I thought about it is sending the parent state down as props via callback, but I can't find a way to send props through the screen components in the up-to-date version of react navigation...

This is my navigation setup:

const Tab = createBottomTabNavigator()

export default class MyApp extends Component{

    constructor(props) {
        super(props);
    }

    render(){
        return (
            <NavigationContainer>
                <Tab.Navigator 
                    screenOptions={({ route }) => ({
                        tabBarIcon: ({ focused, color, size }) => {
                            let iconName;

                            if (route.name === 'My tests') {
                                iconName = focused ? 'ios-list-box' : 'ios-list';
                            } else if (route.name === 'Testroom') {
                                iconName = focused ? 'ios-body' : 'ios-body';
                            }

                            return <Ionicons name={iconName} size={size} color={color} />;
                        },
                    })}
                    tabBarOptions={{
                        activeTintColor: 'tomato',
                        inactiveTintColor: 'gray',
                            
                    }}>

                    <Tab.Screen name="My tests"  component={MyTests}/> //This is where I want to send props
                    <Tab.Screen name="Testroom" component={TestRoom} />  //This is where I want to send props
                    
                </Tab.Navigator>
            </NavigationContainer>
        )
    }
}

I've read this but it makes no sense to me. How does this fit into a tree of components? What goes where? Please help!

weedskurt
  • 451
  • 1
  • 4
  • 4
  • Does this answer your question? [React Native - pass props from One screen to another screen (using tab navigator to navigate)](https://stackoverflow.com/questions/57186298/react-native-pass-props-from-one-screen-to-another-screen-using-tab-navigator) – 8888 Apr 09 '20 at 15:24
  • @CaseSilva see if this helps you https://stackoverflow.com/a/61112784/11725995 – 8888 Apr 09 '20 at 15:25
  • Not really since I would need to create the component that uses the data in the main App component and I don't think it should be created explicitly there. The component that needs the value of the state also needs to communicate with its parent component which is further down the stack/tab navigation tree. – Case Silva Apr 09 '20 at 17:45
  • 1
    I had a very similar situation and ended up using React's Context API. Check to see if this will work for your needs: https://stackoverflow.com/a/61196113/6581190 – Case Silva Apr 13 '20 at 20:26

11 Answers11

79

you can use the property 'children' to pass an element type jsx like instead of the property 'component', is recomended from react-native.

On this way you can pass props to component example:

    <tab.Screen
    name="home"
       children={()=><ComponentName propName={propValue}/>}
    />

is neccesary to use '()=>' because the property children need a function that return a jsx element, it's functional.

Oscar Urueta
  • 791
  • 5
  • 4
20

Check out the answer in the code comments.

<Tab.Screen
  name="AdminTab"
  children={() => <AdminPage userData={this.props.userSettings} />}
  // component={() => <AdminPage userData={this.props.userSettings} />} <<<---- Although this will work but passing an inline function will cause the component state to be lost on re-render and cause perf issues since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour. You can safely remove the component attribute post adding children.
/>

Looks like you're passing an inline function for 'component' prop for the screen 'AdminTab' (e.g. component={() => <SomeComponent />}). Passing an inline function will cause the component state to be lost on re-render and cause perf issues since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour.

Rishav
  • 3,818
  • 1
  • 31
  • 49
15

React Navigation version 5+

<Tab.Screen name="Work" component={Works} initialParams={{ age: 45 }} />

Can be accessed using props on the Works component.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Joseph Owigo
  • 430
  • 3
  • 10
  • 4
    You the man now, Dawg! This was the trick. Thanks. Just FYI, inside of the `Works` Component, you can access `age` (or any `initialParams`) via `this.props.route.params.age`. – Joshua Pinter Oct 03 '21 at 19:38
9

In order to send props to <MyTests /> component, inside <Tab.Screen />:

<Tab.Screen name="My tests">
  {() => <MyTests myPropsName={myPropsValue} />}
</Tab.Screen>
2

I also need to pass a function to screen in order to render the popup on all screens but it is required to show the popup on a specific screen. here is my solution. Now forget about the re-render (performance) issue.

       <Tab.Screen
        initialParams={{
            onRemovePress: () => {
              props.setShowPopup(true)
            }
          }}
        name="Followers"
        component={Followers}
        options={{
          tabBarLabel: ({ focused, color }) => {
            return (
              <Text style={{ color: focused ? light.tintColor : light.black, marginBottom: 10 }}>{`${props.numOfFollowers} Followers`}</Text>
            )
          }
        }}
      />
AliRehman7141
  • 873
  • 3
  • 12
  • 33
2
<Tab.Navigator>
   <Tab.Screen name="Lead">
   {() => <LeadList {...this.props} />}
   </Tab.Screen>

   <Tab.Screen name="Opportunity">
   {() => <OpportunityList {...this.props} />}
   </Tab.Screen>
</Tab.Navigator>
Vivek
  • 4,916
  • 35
  • 40
0

you can use the following

<Tab.Screen name="Favourite" component={() => <RecipeList CategoryId={0}></RecipeList>}/>

You may gate a warning loss of current state due to re-render the component. but the props will work. If you want to copy current state props then you can use {...props}

  • 1
    Passing an inline function will cause the component state to be lost on re-render and cause perf issues since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour. – Rishav Aug 23 '20 at 19:54
0
<Tab.Screen
   name='tabName'
   children={()=>{
    return(
      <CustomComponent prop={yourProp} />
    )
   }}
  />

Was looking into this first time today, and found it very helpful just wanted to give small update, for @react-navigation/bottom-tabs, v^5.11.10, you need to make sure you are no longer passing in the component prop in Tab.screen and make sure the anonymous function in children, returns an element/component.

0

In case you are using ScreenMap to render screens you could do it like this:

_renderScene = SceneMap({
    first: () => <MyEvents {...this.props} date={this.state.date} />,
    second: () => <F4FEvents {...this.props} date={this.state.date} />,
});
Arvind K.
  • 1,184
  • 2
  • 13
  • 27
0

if you navigate from one tab to another and not updating please try this method this will help you

 <Tab.Screen
    name="Transaction"
    children={() => <GoldTransaction data={true} />}/>
0

this works in JS ,but not works in TS. Because TS expects type comes from property 'children' which is declared here on type children: (props: { route: RouteProp<ParamList, RouteName>; navigation: any;› }) => React.ReactNode;

export declare type ScreenListeners<State extends NavigationState, EventMap extends EventMapBase> = Partial<{
    [EventName in keyof (EventMap & EventMapCore<State>)]: EventListenerCallback<EventMap, EventName>;
}>;
declare type ScreenComponentType<ParamList extends ParamListBase, RouteName extends keyof ParamList> = React.ComponentType<{
    route: RouteProp<ParamList, RouteName>;
    navigation: any;
}> | React.ComponentType<{}>;
export declare type RouteConfigComponent<ParamList extends ParamListBase, RouteName extends keyof ParamList> = {
    /**
     * React component to render for this screen.
     */
    component: ScreenComponentType<ParamList, RouteName>;
    getComponent?: never;
    children?: never;
} | {
    /**
     * Lazily get a React component to render for this screen.
     */
    getComponent: () => ScreenComponentType<ParamList, RouteName>;
    component?: never;
    children?: never;
} | {
    /**
     * Render callback to render content of this screen.
     */
    children: (props: {
        route: RouteProp<ParamList, RouteName>;
        navigation: any;›
    }) => React.ReactNode;
    component?: never;
    getComponent?: never;
Kay
  • 1
  • i resolved by this method in TS > get a navigation prop for your tabBar https://reactnavigation.org/docs/material-top-tab-navigator/ – Kay Feb 24 '23 at 02:41