So I'm trying to build a small search app. The problem I'm encountering is that I can't refetch the query when the state of text input changes. here's the code
getInitialState: function(){
return {
searchParams: '' "
}
},
render: function(){
return (
<View style={styles.container}>
<SearchBar goSearch={this.goSearch}/>
<SearchResults searchParams={this.state.searchParams} />
</View>
)
},
goSearch: function(searchParams){
this.setState({searchParams: searchParams});
}
So I have this parent component that has searchBar component that's the search bar where user enters the search query, and searchResults component that renders the results. Go Search function as u can see changes the state whenever the text changes in the searchBar component. Now for the searchResults component..
componentWillReceiveProps(nextProps){
if(this.props.searchParams!=nextProps.searchParams){
console.log("props changed " + nextProps.searchParams)
this.props.data.refetch();
console.log(this.props.data.refetch)
}
},
render: function(){
return (
<View>
{this.renderPosts()}
</View>
)
},
module.exports = graphql(fetchFilterPosts, {
options: (props) => {
return {
variables: {searchParams:props.searchParams}
}
}
})(SearchResults);
What im trying to do here is that when the state of the parent component changes, It will be passed in the searchResult component as a prop, and it's passed as a variable in the graphQL query. Now the problem is that I can't refetch it when the props change. I have setup componentWillReceiveProps to check if the props changes and refetch the data but it doesn't seem to work. I've Read some articles about subscriptions and updateQueries, but I'm not sure if thats the solution I'm looking for. Need desperate help. Thanks in advance