T
T
thehighhomie2020-06-02 21:05:07
React
thehighhomie, 2020-06-02 21:05:07

React: Why is the imported variable in the useCallback hook not defined?

There is a file myModule.js:

let myModule = 500;
export { myModule };


and there is a component that imports a variable from the module and overrides it in the useCallback hook:
import React, { useCallback } from "react";
import { myModule } from "./myModule";

export const MyComponent = () => {
  const myRef = useCallback(() => {
    myModule = 500; // ошибка, так как переменная не определена
  }, []);

  return <div ref={myRef} />;
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-02
@thehighhomie

it's just an import, what's the point of overwriting it, it contains the value from the file. Create another variable, initialize it with the value of myModule, and overwrite it as much as you like.
If you want to change the value of a variable in the file you are importing from, then create a function there, import it and call it, passing the desired parameter, and it will already change the value of the variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question