React native set state from drawer navigator to other component

假装没事ソ 提交于 2020-01-06 03:04:08

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/53087325/react-native-set-state-from-drawer-navigator-to-other-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!