Hemadri Dasari
Hemadri Dasari

Reputation: 34014

React: Cannot ready property 'readContext' of undefined

react-cache is not working with Suspense.

My code

  import React, { Suspense } from "react";
  import ReactDOM from "react-dom";
  import { unstable_createResource as createResource } from "react-cache";

    const MarkdownCache = createResource(input => {
       return new Promise(resolve => resolve(input));
    });

    const App = () => {
        return (
           <Suspense fallback={<div>Loading...</div>}>
               <Test />
           </Suspense>
        );
    }

    const Test = () => {
           const input = MarkdownCache.read("Test react cache");
           return input;
     }

const rootElement = document.getElementById("root"); ReactDOM.render(, rootElement);

Versions I am using:

  react: 16.8.0-alpha.0
  react-dom: 16.8.0-alpha.0
  react-cache: 2.0.0-alpha.1

Upvotes: 1

Views: 1754

Answers (3)

Nilesh Kadam
Nilesh Kadam

Reputation: 27

Modify/Replace the code in 'react-cache/cjs/react-cache.development.js'

OLD -:

var currentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;

function readContext(Context, observedBits) {
  var dispatcher = currentOwner.currentDispatcher;
  if (dispatcher === null) {
    throw new Error('react-cache: read and preload may only be called from within a ' + "component's render. They are not supported in event handlers or " + 'lifecycle methods.');
  }
  return dispatcher.readContext(Context, observedBits);
}

NEW -:

const ReactCurrentDispatcher =
  React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
    .ReactCurrentDispatcher;

function readContext(Context, observedBits) {
  const dispatcher = ReactCurrentDispatcher.current;
  if (dispatcher === null) {
    throw new Error(
      'react-cache: read and preload may only be called from within a ' +
        "component's render. They are not supported in event handlers or " +
        'lifecycle methods.',
    );
  }
  return dispatcher.readContext(Context, observedBits);
}

Upvotes: 2

Ganapati V S
Ganapati V S

Reputation: 1661

Current alpha of [email protected] is not compatible with newly published [email protected] and [email protected].

Downgrade to [email protected] and [email protected] till new compatible alpha version of react-cache is released.

Upvotes: 2

Hemadri Dasari
Hemadri Dasari

Reputation: 34014

The workaround for this problem I found from the internet is...

If you just want to run the program in the development env, you can modify the code in 'react-cache/cjs/react-cache.development.js' by yourself: old:

    var currentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;

    function readContext(Context, observedBits) {
        var dispatcher = currentOwner.currentDispatcher;
        if (dispatcher === null) {
        throw new Error('react-cache: read and preload may only be called from within a ' + "component's render. They are not supported in event handlers or " + 'lifecycle methods.');
       }
       return dispatcher.readContext(Context, observedBits);
     }

'currentOwner' is no use except in function readContext. so here is the new:

      var currentDispatcher =      React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher;

     function readContext(Context, observedBits) {
          var dispatcher = currentDispatcher.current;
          if (dispatcher === null) {
               throw new Error('react-cache: read and preload may only be called from within a ' + "component's render. They are not supported in event handlers or " + 'lifecycle methods.');
           }
          return dispatcher.readContext(Context, observedBits);
         }

And that work in my code.

Upvotes: 1

Related Questions