1
votes

On first screen I have a component having some listing of products say ProductListing.js. And on drawer navigator I have some check boxes. I want to set state of ProductListing.js when I am clicking on any check box of navigator.

App.js

import React, {Component} from 'react';
import Router from './src/config/routes';
export default class App extends React.Component {
  render () {
    return (
      <Router/>
    );
  }
}

Router.js

export default DrawerNavigator({
Dashboard: {screen: Dashboard},
   CreateLogin: {screen: CreateLogin},
   Test: {screen: Test}
}, {
   contentComponent: SideMenu,
   drawerWidth: 300,
   drawerPosition: 'right'
 });

SideMenu.js

render () {
  const { data, searchTerm, searchAttribute, ignoreCase, checked } = this.state;
    return (
      <View style={styles.container}>
        <ScrollView>
          <View>
          <TextInput
                style={styles.search} placeholder={"Search"}
                onChangeText={searchTerm => this.setState({ searchTerm })} />

              <SearchableFlatList
                 data={data} searchTerm={searchTerm}
                searchAttribute={searchAttribute} ignoreCase={ignoreCase}
                renderItem={({ item, index }) => <CheckBox
                                                          title={item.name +' ('+ item.count+')'}
                                                          onPress={() => this.handleChange(item.id)}
                                                          checked={checked[item.id]} />
                                                      }
                keyExtractor={({id}, index) => index.toString()} />
          </View>
        </ScrollView>
        <View style={styles.footerContainer}>
          <Text>Apply filter by choosing filter values</Text>
        </View>
      </View>
    );
  }
}

ProductListing.js

constructor(props){
            super(props);
            this.state ={ isLoading: true,isloadmore: false, page :1,dataSource: [],countryFilter:0, gradesFilter:'',country:'',totalRecord:0};
       }

render(){
const { navigation } = this.props;
           return(
 <View style={{flexDirection:'column',paddingRight:8}}>
                        <Button
                          title='Filter'
                          buttonStyle={{backgroundColor:'#000000',borderRadius:2}}
                          onPress={() => navigation.dispatch(DrawerActions.openDrawer())}
                        />
                   </View>
 );

       }

Now on click of handleChange in SideMenu.js I want to update state of gradesFilter in ProductListing.js where I am updating my product listing.

2

2 Answers

0
votes

You can easily achieve this with a portal! Take a look here.

0
votes

You can pass the parameters from drawer to the Product screen via navigation params. For this, you need to stackNavigator inside your DrawerNavigator like: Router:

const MyStack = StackNavigator({
  Dashboard: {screen: Dashboard},
  CreateLogin: {screen: CreateLogin},
  Test: {screen: Test}
});

const MyDrawer = DrawerNavigator({
  Main: { screen: MyStack }
},
  {
   contentComponent: SideMenu,
   drawerWidth: 300,
   drawerPosition: 'right'
 });

export const Router = StackNavigator({
  Login: {screen: Login},
  Drawer: {
     screen: MyDrawer,
     navigationOptions: { header: null } } //prevent double header
});

Now you can navigate and pass parameter from login or Dashboard or anyother screen via Drawer like

_login() {
    this.props.navigation.navigate('Drawer', { parameter: userName });
}

And to use this parameter you need to access:

  const { parameter } = this.props.navigation.state.params;

which you can further use to set state like

this.setState({
productSelection: parameter)};

Another approach can be via, listener/ Callback handler.