1

I'm having some trouble with a piece of code in React.

I've created a gh repo that reproduce the error : https://github.com/AnatoleLucet/next-issue

I'm trying to do a hook that gets some local data (in my real app it's a translation system, but you get the idea). Thing is, I pass each "part" of my data (see data/myData.ts and data/index.ts) in a function called formatData, this function is declared in the same file as my hook (hooks/myHook.ts). Then when I use my hook in a component (see pages/index.tsx) I get the following error :

I don't understand why the browser (or webpack ?) doesn't initialize formatData at the right time...

I'm using Nextjs but I don't think it's related to the issue, although I haven't tried on a "vanilla" React project.

As a temporary fix, I can move formatData to another file like utils/data.ts.

1 Answer 1

3

You have a circular dependency.

  • hooks/myHook.ts imports from data/index.ts
  • data/index.ts imports from data/myData.ts
  • data/myData.ts imports from hooks/myHook.ts
1
  • Indeed. So I have to rethink my design. Thanks Commented Oct 29, 2020 at 10:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.