Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: calledOnce is not defined #28

Closed
catamphetamine opened this issue Aug 6, 2018 · 9 comments
Closed

ReferenceError: calledOnce is not defined #28

catamphetamine opened this issue Aug 6, 2018 · 9 comments
Labels

Comments

@catamphetamine
Copy link

When used with react-hot-loader it throws this error

VM37389:13 Uncaught ReferenceError: calledOnce is not defined
    at ProxyComponent.result [as getDateFilterMinDateRefinement] (eval at __reactstandin__regenerateByEval (VM36190 CourseSearch.js:NaN), <anonymous>:13:5)
    at eval (eval at __reactstandin__regenerateByEval (VM36190 CourseSearch.js:NaN), <anonymous>:45:36)
    at ProxyFacade (react-hot-loader.development.js:647)
    at mountIndeterminateComponent (react-dom.development.js:13380)
    at beginWork (react-dom.development.js:13820)
    at performUnitOfWork (react-dom.development.js:15863)
    at workLoop (react-dom.development.js:15902)
    at HTMLUnknownElement.callCallback (react-dom.development.js:100)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:138)
    at invokeGuardedCallback (react-dom.development.js:187)

getDateFilterMinDateRefinement() is a function that's wrapped in memoize().

The error happens, for example, when the component containing memoization is removed from code and then hit "Save" so that it hot-reloads.

@alexreardon
Copy link
Owner

I am a regular user of hot reloading and I have not experienced this issue.

There is something funny going in on that hot reload. Take a look at the code there is not much, and I cannot see how a variable (calledOnce) in a closure could be trashed. It sounds like there is something going on that is not respecting standard scoping which is a problem. regenerateByEval might be causing some issues as this might be losing the function scope of the returned function.

This feels like it is an issue with the hot reloader. Can you please post an example of how you are using memoizeOne? Cheers

@catamphetamine
Copy link
Author

catamphetamine commented Aug 6, 2018

We're using it in constructor:

export default class CourseSearch extends Component {

  constructor() {
    super();
    // Re-rendering performance optimization.
    // But has a bug with `react-hot-loader`.
    this.getDateFilterMinDateRefinement = memoize(this.getDateFilterMinDateRefinement);
    this.getInstitutionFilterForSpecificInstitution = memoize(this.getInstitutionFilterForSpecificInstitution);
  }

  getDateFilterMinDateRefinement = min => ({ min })
  getInstitutionFilterForSpecificInstitution = institution => [institution]

  render() {
    return (

        <InstantSearchExcludePast
          ...
          defaultRefinement={this.getDateFilterMinDateRefinement(today + addDropDateShift)}
          today={today + addDropDateShift} />

        <InstantSearchMultiSelectList
          ...
          defaultRefinement={institution ? this.getInstitutionFilterForSpecificInstitution(institution) : undefined} />

    )
  }
}

The point is that while props stay the same by reference React doesn't re-render (probably, or maybe it still does).

I'll close this for now, as it seems like an our way of writing code + hot-loader + memoizeOne all combined in a single use case.

@nihgwu
Copy link
Contributor

nihgwu commented Sep 6, 2018

I'm encountering exactly the same problem, do you find a solution @catamphetamine

@catamphetamine
Copy link
Author

catamphetamine commented Sep 6, 2018 via email

@nihgwu
Copy link
Contributor

nihgwu commented Sep 6, 2018

Right now I have to workaround like this this.method = module.hot ? this.method : memoize(this.method)

@nihgwu
Copy link
Contributor

nihgwu commented Sep 6, 2018

So if this problem still exists, I'd suggest to reopen the issue, no matter what's the root cause, but we have to find the reason

@alexreardon
Copy link
Owner

memoize-one is just a function that returns a new function: a simple higher order function. It looks like the scope of the returned function is being lost in a hot reloading process. This is a problem with the hot reloader as no scope should be destroyed.

It looks like this is the cause: gaearon/react-hot-loader#978

@alexreardon alexreardon reopened this Sep 6, 2018
@alexreardon
Copy link
Owner

Happy to reopen but it looks like the issue is not with this library, but with the hot reloaders ability to deal with higher order functions

@nihgwu
Copy link
Contributor

nihgwu commented Sep 7, 2018

@alexreardon thanks for the context, it's really helpful, I can confirm the error is gone after upgrade react-hot-loader to ^4.3.0, but seems the memoized function won't be reloaded any more, but that's not the problem of this library (UPDATE: that's intended gaearon/react-hot-loader#1002)

Thanks for your great library 👍 , I think it's good to close now, but change the label to question in case the others meet the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants