solid-router icon indicating copy to clipboard operation
solid-router copied to clipboard

"Uncaught ReferenceError: React is not defined" when using Router

Open kee-oth opened this issue 3 years ago • 3 comments

I'm getting Uncaught ReferenceError: React is not defined when running the below code. I'm not sure why React is in the equation here. What am I missing about how this works?

import { Router, pathIntegration } from '@rturnq/solid-router';

const Root = () => {
  return (
    <Router integration={pathIntegration()}>
      <h1>App</h1>
    </Router>
  );
};

export default Root;

Versions:

"@rturnq/solid-router": "^0.2.6",
"solid-js": "^0.26.5",

Thanks!

kee-oth avatar May 22 '21 20:05 kee-oth

You code looks fine. Are you using Vite?

Assuming this is the case. Vite uses esbuild to pre-build dependencies as an optimization. By default esbuild treats any JSX it encounters as React and converts it to React.createElement calls. Vite excludes dependencies it knows have JSX but only does so by looking at the extension of the entry file which doesn't work in this case. It does have a workaround though by manually excluding the library using the optimizeDeps.exclude config option.

// vite.config.js
export default defineConfig({
  ...
  optimizeDeps: {
    exclude: ['@rturnq/solid-router'],
  }
});

rturnq avatar May 23 '21 00:05 rturnq

I am using Vite and your suggested fix works!

Is this in the docs somewhere? I just took a look and wasn't able to find this information.

Thank you!

kee-oth avatar May 23 '21 01:05 kee-oth

There is now https://github.com/rturnq/solid-router/commit/ffa31c845966d351e211673456d117a03846261c

rturnq avatar May 23 '21 06:05 rturnq