Using the old way:
graphql(GET_PROJECTS_BY_USER_ID, {
options: ownProps => ({
skip: !ownProps.loggedUser.id,
}),
props: ({ data }) => {
return data;
},
});
I could catch the list of projects inside componentWillReceiveProps
lifecycle and dispatch the redux action there:
componentWillReceiveProps(nextProps) {
if (!nextProps.loading && this.props.loading) {
this.props.setProjects(nextProps.getProjects);
}
}
Now I'm trying out Apollo's <Query/>
component and I can't figure out how to properly dispatch an action:
render() {
return (
<Query
query={GET_PROJECTS_BY_USER_ID}
skip={!this.props.loggedUserId}
>
{({ loading, data: { getProjects: projects } }) => {
if (loading) return 'Loading ...';
this.props.setProjects(projects); // no good
// return projects
}}
</Query>
);
}
This will cause the following warning:
Cannot update during an existing state transition (such as within
render
or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved tocomponentWillMount
.
The problem is that by using <Query>,
componentWillMount` will no longer be called.