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

commonjs and esm import mismatch #176

Closed
raph5 opened this issue Mar 30, 2024 · 12 comments
Closed

commonjs and esm import mismatch #176

raph5 opened this issue Mar 30, 2024 · 12 comments
Labels
wontfix This will not be worked on

Comments

@raph5
Copy link

raph5 commented Mar 30, 2024

Describe the bug
Im using a server side rendering react framework (remix) and TreeView server side rendering is not working.
When I try to use TreeView component in my projet it work just fine on client after hydration but when I reload the page (and remix try to ssr the component) I get this errro : Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

Code That Causes The Issue
After searching a little I found that the error came from commonjs import :
I tried to console log the module

import * as module from "react-accessible-treeview"
console.log(module)

And here is the results on the server :

{
  CLICK_ACTIONS: [ 'SELECT', 'FOCUS', 'EXCLUSIVE_SELECT' ],
  __esModule: true,
  default: {
    CLICK_ACTIONS: [ 'SELECT', 'FOCUS', 'EXCLUSIVE_SELECT' ],
    default: {
      '$$typeof': Symbol(react.forward_ref),
      render: [Function (anonymous)],
      propTypes: [Object]
    },
    flattenTree: [Function (anonymous)]
  },
  flattenTree: [Function (anonymous)]
}

As on the client I get :

{
  CLICK_ACTIONS: [ 'SELECT', 'FOCUS', 'EXCLUSIVE_SELECT' ],
  default: {
    '$$typeof': Symbol(react.forward_ref),
    render: [Function (anonymous)],
    propTypes: [Object]
  },
  flattenTree: [Function (anonymous)]
}

This mismatch come from the fact that server uses dist/react-accessible-treeview.cjs.js and client uses dist/react-accessible-treeview.esm.js

Here is the code i implemented to fix the issue on my project :

import * as module from "react-accessible-treeview"

let TreeView: typeof module.default
let flattenTree: typeof module.flattenTree
let CLICK_ACTIONS: typeof module.CLICK_ACTIONS

if(typeof document == "undefined") {
  TreeView = module.default.default
  flattenTree = module.default.flattenTree
  CLICK_ACTIONS = module.default.CLICK_ACTIONS
}
else {
  TreeView = module.default
  flattenTree = module.flattenTree
  CLICK_ACTIONS = module.CLICK_ACTIONS
}

export { flattenTree, CLICK_ACTIONS }
export default TreeView

To Reproduce
create a new remix project : npx create-remix@latest
install react-accessible-treeview (im using pnpm) : pnpm add react-accessible-treeview
implement basic exemple from the doc https://dgreene1.github.io/react-accessible-treeview/docs/examples-Basic

Desktop :

  • OS: windows 11 running wsl2
  • Browser: Chrome
@dgreene1
Copy link
Owner

We would be open to a PR for this, but first a few things of concern:

  • I believe the code you’re talking about is handled by our bundled so I’m not sure it can be changed. Maybe you’ll find it can
  • To accept a PR for this, we’d want to make sure that we have test for this so that it doesn’t regress

@raph5
Copy link
Author

raph5 commented Mar 30, 2024

Personally I will not take the time to search futher because the fix works for my project.
Thank you for this component. I had a lot of trouble finding a good lightweight tree view component as big headless ui frameworks don't implement them.

Copy link

stale bot commented May 29, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 29, 2024
Copy link

stale bot commented Jun 5, 2024

This issue was closed automatically since it was marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot closed this as completed Jun 5, 2024
@ersinakinci
Copy link

Just chiming in that I also have this problem on my Remix project.

@dgreene1
Copy link
Owner

Open to a PR

@dgreene1 dgreene1 reopened this Jul 15, 2024
@stale stale bot removed the wontfix This will not be worked on label Jul 15, 2024
@Ahtisham-Shakir
Copy link

Ahtisham-Shakir commented Aug 23, 2024

Hi, i'm also facing the same issue in my remix project. I've tried the above solution but it's not working for me. I've used the dynamic import to solve the issue.

const TreeView = lazy(() => import('react-accessible-treeview').then(module => ({ default: module.default })));

@kvandake
Copy link

kvandake commented Oct 7, 2024

Hi, i'm also facing the same issue in my remix project. I've tried the above solution but it's not working for me. I've used the dynamic import to solve the issue.

const TreeView = lazy(() => import('react-accessible-treeview').then(module => ({ default: module.default })));

this solution doesn`t work for me...

Copy link

stale bot commented Dec 7, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Dec 7, 2024
Copy link

stale bot commented Dec 14, 2024

This issue was closed automatically since it was marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot closed this as completed Dec 14, 2024
@dgreene1
Copy link
Owner

We’re investigating this next month.

@dgreene1 dgreene1 reopened this Jan 29, 2025
Copy link

stale bot commented Feb 6, 2025

This issue was closed automatically since it was marked as stale because it has not had recent activity. Thank you for your contributions.

@stale stale bot closed this as completed Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants