Skip to content

useMutation passes undefined "dependency" to class that returns a promise #3228

@benacq

Description

@benacq

Describe the bug

  • I have a repository class and a use-case class(DataRepository and PostUseCase)
  • Data repository implements a base repository interface(IRepository).
  • The use-case has a 'addNewPost' method which depends on the repository.
  • addNewPost calls and returns a "post method" on the injected repository and returns the promise.
  • The problem here is, when I log the injected repository inside the addNewPost method, I get undefined.
  • Meaning the repository I injected is not reaching the use-case for some reason, but when I call the post method on the repository directly as my mutationFn, it works fine but it doesn't work when I inject the repository and have the use-case call the post method instead.
  • I replicated the issue here, added a few comments, please let me know

Your minimal, reproducible example

https://codesandbox.io/s/react-typescript-forked-ijem7?file=/src/some-component.tsx

Steps to reproduce

  • On the index page, click on the "Add post" button(payload is hardcoded)
  • Check useMutation hook in src/some-component and uncomment the commented mutationFn to see how they behave.
  • A successful request will display success on the screen, an error will display error on the screen otherwise you will see idle
  • Please check the console when you get an error status.
  • Please read the comments to get more context.
  • repository can be found in src/repository
  • use-case can be found in src/use-cases

Expected behavior

I expect react-query to inject the repository dependency to the use-case so that calling the post function will be deligated to the use-case and react-query will rather depend on the use-case for the Promise

How often does this bug happen?

Every time

Screenshots or Videos

Screenshot from 2022-01-25 13-59-52

Platform

  • OS: Linux
  • Browser: Chrome
  • Version: Version 97.0.4692.71 (Official Build) (64-bit)

react-query version

v3.34.11

TypeScript version

v4.5.4

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions