-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: update subscribeToMore for getDerivedStateFromProps #3680
Conversation
brainkim
commented
Jul 12, 2018
•
edited
Loading
edited
- docs
@brainkim: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/ |
I can force-push rebase whenever you want me too. @hwillson |
Hi @brainkim - first off, thanks for working on this! I'm not a huge fan of copying props into state, whenever it can be avoided. In this case, instead of using class SubscriptionComponent extends Component {
constructor(props) {
super(props);
this.unsubscribe = null;
}
componentDidMount() {
if (!this.props.loading) {
this.initSubscription();
}
}
componentDidUpdate(prevProps) {
if (!this.props.loading) {
if (this.unsubscribe &&
(prevProps.subscriptionParam !== this.props.subscriptionParam)
) {
this.unsubscribe();
this.unsubscribe = null;
}
this.initSubscription();
}
}
componentWillUnmount() {
if (this.unsubscribe) {
this.unsubscribe();
this.unsubscribe = null;
}
}
initSubscription() {
if (!this.unsubscribe) {
this.unsubscribe = this.props.data.subscribeToMore({
document: gql`subscription {...}`,
updateQuery: (previousResult, { subscriptionData, variables }) => {
// Perform updates on previousResult with subscriptionData
return updatedResult;
}
});
}
}
render() {
...
}
} That should help tick all of the boxes the previous example was handling, and get us away from copying props into state. If you're interested in updating this PR to follow something more along these lines, that would be awesome! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments in #3680 (comment) - thanks!
Hmmm. I agree that duplicating props in state is worrying but I think it might be necessary here b/c of edge cases relating to TBH I don't have a 100% understanding of the correct use cases for If you disagree, feel free to close the PR and merge a different one! |
Pinging @hwillson any thoughts? I checked your code and it doesn’t work even in the happy path case b/c by the time |
Thanks for the feedback @brainkim - I'll take another look at this shortly. For now let's leave this open though. I agree with you that this should be covered in the docs, so we'll get something in place shortly. Thanks for your patience! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do it @brainkim - thanks very much for working on this!
Thank you for all the work you do on Apollo! |
I have a few questions about this update:
|
|