Reputation: 525
Hello I am creating react native application. In this app homepage contains list of projects and a filter button. When I click filter button filter screen will open and that screen contain 3 filter drop downs. User will select value from dropdown and click on filter button. Once user click on filter button page should be redirected to home page and the list should be regenerated with new filter data. Now I wanted to pass filter value to child component of home page, but I am not getting how to send data to child component of homepage. I can get all my filter value in console window, but I don't know how should I pass that value to to render new data on component.
Here is my code:
HomeScreen.js
render() {
return (
<View style={styles.container}>
<HomeProject />
<View>
<TouchableHighlight
style={styles.addButton}
underlayColor='#ff7043' onPress={() => NavigationService.navigate('Filters', { filterCallback: filterValue => this.onFilterCallback(filterValue) })}>
<Text style={{ fontSize: 20, color: 'white' }}>+</Text>
</TouchableHighlight>
</View>
</View>
);
}
onFilterCallback(filterValue) {
console.log('-> Callback value:', filterValue);
this.setState({ filterValue: filterValue });
}
HomeProject.js
class HomeProject extends Component {
constructor(props) {
super(props)
}
componentWillMount() {
this.props.fetchProjectList();
this.createDataSource(this.props);
}
componentWillReceiveProps(nextProps) {
console.log('nextProps' + JSON.stringify(nextProps));
console.log('receive' + JSON.stringify(nextProps.filterValue));
this.createDataSource(nextProps);
}
createDataSource({ projectlist }) {
console.log('111');
console.log('projetclist' + projectlist);
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(projectlist);
}
renderRow(data) {
console.log('222');
const { currentUser } = firebase.auth();
// if (data.userid !== currentUser.uid && !data.isDraft) {
return (<ProjectList data={data} />);
// }
}
render() {
return (
<View style={{ flex: 1 }}>
<ListView
style={{ flex: 1 }}
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
</View>
);
}
}
FilterScreen.js
onGoBack() {
console.log('Filter is in going back');
const { userprofile } = this.props;
const { type } = userprofile;
console.log('type :' + type);
const ref = firebase.database().ref('projects');
const query = ref.orderByChild('type').equalTo(type);
query.on('value', (snapshot) => {
console.log('project detail ', snapshot.val());
const filterProjects = [];
snapshot.forEach((item) => {
filterProjects.push({ key: item.key,
userid: item.val().userid,
title: item.val().title,
location: item.val().location
});
});
console.log("filterProjects: ", filterProjects);
if (this.params && this.params.filterCallback) this.params.filterCallback(filterProjects);
console.log('goback');
this.props.navigation.goBack();
}
renderButton() {
return (
<Button
style={{ alignItems: 'center' }}
onPress={this.onGoBack.bind(this)}
>
Filter
</Button>
);
}
render() {
const { navigate } = this.props.navigation
return (
<ScrollView style={{ flex: 1, backgroundColor: '#ffffff' }}>
{this.renderLoading()}
<DropDown
label="Project Type"
containerStyle={{
width: 100,
//zIndex: 60,
top: 20,
}}
onValueChange={(value) => this.props.userProfile({ prop: 'type', value })}
selectedValue={this.props.userprofile.type}
>
{Object.keys(this.props.types).map((key) => {
return (<Picker.Item
label={this.props.types[key]}
value={this.props.types[key]}
key={key}
/>);
})}
</DropDown>
<DropDown
label="Language"
containerStyle={{
width: 140,
//zIndex: 60,
top: 20,
}}
onValueChange={(value) => this.props.userProfile({ prop: 'category', value })}
selectedValue={this.props.userprofile.category}
>
{Object.keys(this.props.categories).map((key) => {
return (<Picker.Item
label={this.props.categories[key]}
value={this.props.categories[key]}
key={key}
/>);
})}
</DropDown>
<DropDown
label="Keywords"
containerStyle={{
width: 140,
//zIndex: 60,
top: 20,
}}
onValueChange={(value) => this.props.userProfile({ prop: 'category', value })}
selectedValue={this.props.userprofile.category}
>
{Object.keys(this.props.categories).map((key) => {
return (<Picker.Item
label={this.props.categories[key]}
value={this.props.categories[key]}
key={key}
/>);
})}
</DropDown>
<CardSection style={styles.filterBtnStyle}>
{this.renderButton()}
</CardSection>
</ScrollView>
);
}
Upvotes: 1
Views: 208
Reputation: 1227
Why don't you just use the props to give your filterValue to the child component ?
In your HomeScreen.js :
<HomeProject filterValue={this.state.filterValue}/>
And then you can access it in your HomeProject component with :
this.props.filterValue
You can pass whatever you want as props, and use them in child components. When setState is called in the parent component, it forces to rerender children with the new value.
You can read more about props on Facebook documentation: https://facebook.github.io/react-native/docs/props
Upvotes: 3