在第一个屏幕上,我有一个组件,其中包含一些产品清单,例如ProductListing.js。在抽屉式导航器上,我有一些复选框。当我单击导航器的任何复选框时,我想设置ProductListing.js的状态。
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>
);
}
现在,在SideMenu.js中单击handleChange时,我想更新要更新产品清单的ProductListing.js中gradesFilter的状态。
答案 0 :(得分:0)
您可以通过门户轻松实现这一目标!看一下here。
答案 1 :(得分:0)
您可以通过导航参数将参数从抽屉传递到“产品”屏幕。 为此,您需要像这样在DrawerNavigator中堆叠stackNavigator: 路由器:
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
});
现在,您可以通过Drawer之类的
从登录名,仪表板或任何其他屏幕导航并传递参数_login() {
this.props.navigation.navigate('Drawer', { parameter: userName });
}
要使用此参数,您需要访问:
const { parameter } = this.props.navigation.state.params;
您可以进一步使用它来设置状态,例如
this.setState({
productSelection: parameter)};
另一种方法可以通过侦听器/回调处理程序。