TypeError: Cannot read properties of undefined (reading 'bind')

See original GitHub issue

Bug report

Description / Observed Behavior

When using the useSWR hook like intended, I originally had no problems with it. However, I discovered today that it suddenly threw the error TypeError: Cannot read properties of undefined (reading 'bind').

This error originates from swr/dist/index.mjs at line 153.

Update

This error only occurred to me when @mux/mux-video-react was installed.

Expected Behavior

useSWR runs like normal, and returns my data.

Repro Steps / Code Example

Implemented useSWR in a custom hook called useQuery, which fetches a query from a sanity.io project through a next API. On the server, this query is cached, ensuring as few API calls to Sanity as possible.

import { useRouter } from 'next/router';

import useSWR from 'swr';
import axios from 'axios';

export default function useQuery<T>(query: string, condition?: boolean) {
    // Data from Sanity gets directly linked to the current path in order to revalidate pages that use it
    const router = useRouter();

    const { data } = useSWR<T>(condition !== false? query:null, async () => {
        const res = await axios.get(`/api/query/${query}?path=${router.asPath}`);
        return res.data;
    }

    return data;
}

Additional Context

SWR version - 1.3.0 Axios version - 0.27.2 Next version - 12.2.4 React version - 18.2.0

Until this issue is resolved, I will either prevent usage of SWR in my code, or create my own way of caching the data.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ndimatteocommented, Aug 24, 2022
1reaction
ndimatteocommented, Aug 23, 2022

Update: For whatever reason, this error only appears for me when I install @mux/mux-player-react or @mux/mux-video-react packages via yarn alongside SWR.

If I remove those packages, my Next.js app builds successfully.

Bummed, as I can’t seem to figure out how this has any relation to SWR being used in other components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Cannot read property 'bind' of undefined
There is a typo in onClick on button: this.OnShow.bind(this) is the right way. Function is named OnShow, the state var is named onShow....
Read more >
TypeError: Cannot read property 'bind' of undefined
Traceback: TypeError: Cannot read property 'bind' of undefined at http://localhost:8069/web_editor/static/src/js/wysiwyg/root.js:46:57 at Function._.each._.
Read more >
Uncaught TypeError: Cannot read properties of undefined ...
I binded this method and got an error message: "Cannot read properties of undefined (reading 'bind') " I printed out props with co…...
Read more >
TypeError: Cannot read property 'bind' of undefined
Problem 1: You bound non-existent handleChange and handleSubmit methods to the class without defining either method. Problem 2: Without defining ...
Read more >
Cannot read properties of undefined (reading 'bind') #4308
Prerequisites I have written a descriptive issue title I have searched existing issues to ensure the bug has not already been reported ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found