Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Bug: getClient calls options with with current props #1005

Closed
koenpunt opened this issue Aug 22, 2017 · 1 comment · Fixed by #1025
Closed

Bug: getClient calls options with with current props #1005

koenpunt opened this issue Aug 22, 2017 · 1 comment · Fixed by #1025
Assignees

Comments

@koenpunt
Copy link
Contributor

koenpunt commented Aug 22, 2017

When skip transitions from true to false due to a prop change, the options method is called several times. The times it is called to retrieve for example the variables for the query it is called with the components nextProps. But the internal getClient method calls it with this.props. As this hasn't been brought up as of yet, I recon nobody has used skip in the way I did.

Take for example this options object:

{
  options: ownProps => ({
    variables: {
      gender: ownProps.person.gender,
    },
  }),
  skip: ownProps => !ownProps.person,
}

So as long as there's no person property available I don't want to execute the query.

But the moment the person property is set, options is called several times. Not sure of the order, but at least for skip, variables and client. When it is called for skip or variables, the nextProps are passed, and ownProps.person.gender will return the persons gender.

But when options is called for the client, this.props is passed, which at that point does not yet contain the person prop, and thus accessing ownProps.person.gender will fail with an error.

Intended outcome:

For the client, options should also be called with the value of nextProps

Actual outcome:

App throws an error: Uncaught TypeError: Cannot read property 'gender' of undefined
image

How to reproduce the issue:

I've created a minimal example demonstrating the issue using the error-template (note the branch):
Just run it with the developer console open and the problem will present itself.

https://github.com/koenpunt/react-apollo-error-template/tree/get-client-prev-props

Version

  • apollo-client@1.9.1
  • react-apollo@1.4.14
@jbaxleyiii jbaxleyiii self-assigned this Aug 24, 2017
@jbaxleyiii
Copy link
Contributor

@koenpunt thanks for the detailed usage and report! I'll take a look!

@jbaxleyiii jbaxleyiii mentioned this issue Aug 24, 2017
1 task
jbaxleyiii pushed a commit that referenced this issue Aug 24, 2017
* use up to date props for options, fixes #1005

* changelog

* spelling
jbaxleyiii pushed a commit that referenced this issue Aug 24, 2017
* use up to date props for options, fixes #1005

* changelog

* handle componenDidMount refetch call
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants