Ad

How To Pass A Prop From One Component To Another Component? (React Native)

- 1 answer

I've a Custom Component for Tab View through which I can make dynamic tabs and below is the code for it.

TabView is the custom to make those custom tabs and Selected is the component for a single Tab.

How can I send a prop from TabView component to Selected component?

I know how to send props in a regular scenario, but I don't know to send one in this case.

I made this component from the below link:

https://medium.com/@abdelhalim.ahmed95/how-to-create-scrollable-and-dynamic-top-tabsbar-using-react-navigation-17ca52acbc51

    export class TabView extends Component {

            Tabs = navigation => {

            let tabs=['A', 'B', 'C', 'D','E','F','G','H'];

            tabs = tabs.reduce((val, tab) => {

                  val[tab] = {
                    screen: Selected
                  }
                  return val
            }, {})

            const bottomTabs = createMaterialTopTabNavigator(
                  {
                    ...tabs
                  },
                  {
                    tabBarOptions: {
                      style: {
                        backgroundColor: Themes.colors.FC_PRIMARY,
                      },
                      indicatorStyle:{
                          height: 2,
                          backgroundColor: Themes.colors.TC_PRIMARY_LIGHT,
                      },
                    }
                  }
                )

                const Tabs = createAppContainer(bottomTabs);

                return <Tabs />
          }


          render() {

            const { navigation } = this.props;

            return (
                <View style={STYLES.tabView}>
                    {this.Tabs(navigation)}
                </View>
            );
          }
        }


        export class Selected extends Component {

            constructor(props){
                super(props);
                this.state = {
                  screen: '',
                  screenType: this.props.type
                }
            }


            static navigationOptions = ({ navigation }) => {

                  return({
                    tabBarLabel: ({ focused }) => (

                         <View>        
                              <View style={STYLES.secondaryTabLabel}>
                                 <H3 
                                  type={ focused ? "light" : "disabled" } 
                                  text={navigation.state.routeName} 
                                 />
                              </View>
                         </View>

                    )
                  })

            };

              screenIs = payload => {
                this.setState({ screen: payload.state.routeName })
              }

          render() {

            const { navigation } = this.props;

            return (

              <View style={{flex: 1}}>

                <NavigationEvents onWillFocus={this.screenIs} />

                <Text>{this.state.screen}</Text>

              </View>

            );
          }
        }
Ad

Answer

Use the following code,

       val[tab] = {
                    screen: () => (<Selected val={val}/>) //in case if you want to send val as props
                  }

So the final code of yours will be,

export class TabView extends Component {

        Tabs = navigation => {

        let tabs=['A', 'B', 'C', 'D','E','F','G','H'];

        tabs = tabs.reduce((val, tab) => {

              val[tab] = {
                screen: () => (<Selected val={val}/>), // for props
                navigationOptions: {
                   title: 'Shows' // send anything here to get in navigationOptions
                },
              }
              return val
        }, {})

        const bottomTabs = createMaterialTopTabNavigator(
              {
                ...tabs
              },
              {
                tabBarOptions: {
                  style: {
                    backgroundColor: Themes.colors.FC_PRIMARY,
                  },
                  indicatorStyle:{
                      height: 2,
                      backgroundColor: Themes.colors.TC_PRIMARY_LIGHT,
                  },
                }
              }
            )

            const Tabs = createAppContainer(bottomTabs);

            return <Tabs />
      }


      render() {

        const { navigation } = this.props;

        return (
            <View style={STYLES.tabView}>
                {this.Tabs(navigation)}
            </View>
        );
      }
    }


    export class Selected extends Component {

        constructor(props){
            super(props);
            this.state = {
              screen: '',
              screenType: this.props.type
            }
        }


        static navigationOptions = ({ navigation, navigationOptions }) => {

              return({
                tabBarLabel: ({ focused }) => (

                     <View>        
                          <View style={STYLES.secondaryTabLabel}>
                             <H3 
                              type={ focused ? "light" : "disabled" } 
                              text={navigationOptions.title} // use here 
                             />
                          </View>
                     </View>

                )
              })

        };

          screenIs = payload => {
            this.setState({ screen: payload.state.routeName })
          }

      render() {

        const { navigation } = this.props;

        return (

          <View style={{flex: 1}}>

            <NavigationEvents onWillFocus={this.screenIs} />

            <Text>{this.state.screen}</Text>

          </View>

        );
      }
    }
Ad
source: stackoverflow.com
Ad