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

React hooks composition fail when packaging into different entries #15605

Closed
lavcraft opened this issue Mar 31, 2022 · 8 comments
Closed

React hooks composition fail when packaging into different entries #15605

lavcraft opened this issue Mar 31, 2022 · 8 comments

Comments

@lavcraft
Copy link

Bug report

What is the current behavior?

React hook useContext(Context) doesn't work when call from another hook from different entry

For example:

  1. I have useHook1 and useHook2. useHook2 call useHook1 inside
  2. Configure webpack with two entries - /src/usr-hook1 and /src/usr-hook2` for some reason
  3. Import in main application and use my hooks like this
const Component: React.FC = () => {
    const devices = useHook1();
    const call = useHook2();
    return null
}
export withHook1(withHook2(Component));

Result:

  1. useHook1 - return right value in Component
  2. useHook1 inside useHook2 return undefined <-- problem here

If the current behavior is a bug, please provide the steps to reproduce.
Reproducible example - react-hooks-composition-in-webpack-entry-problem-example

  1. Clone project
  2. npm i
  3. npm run all:start or npm run sdk:dev and then npm run ui:dev in different terminal
  4. open http://localhost:3000 and see console logs

What is the expected behavior?
useHook1 return right value inside useHook2. (In example project names is different, follow code)

Other relevant information:
webpack version: 4.7.0
Node.js version: 16
Operating System: OSX/Windows 11 WSL Ubuntu
Additional tools: next.js 12.1.3

@lavcraft
Copy link
Author

May be I‘m doing something wrong and need read some docs. Please help navigate to right articles for improve understanding of the problem. Now I’m really disoriented

@vankop
Copy link
Member

vankop commented Mar 31, 2022

I don't think this is webpack problem.. please ask in react issue tracker.

@vankop vankop closed this as completed Mar 31, 2022
@lavcraft
Copy link
Author

lavcraft commented Mar 31, 2022

@vankop okay. Ask
facebook/react#24230

I guess it's not a problem. I just need configure webpack right. But don't know how now.

I decide to continue in gitter. Is it a right communication channel with community?. Or another one (which) is more suitable channel?

this - https://gitter.im/webpack/webpack?at=624598776b912423205dd681

@vankop
Copy link
Member

vankop commented Mar 31, 2022

so maybe you just need to add both entrypoints on the page? ( you need to use dependOn option in this case https://webpack.js.org/configuration/entry-context/#dependencies )

@lavcraft
Copy link
Author

lavcraft commented Mar 31, 2022

@vankop this a problem, because i build a library, not a page. But i already tried dependOn :)

How i do it right inside library?

I try use dependOn. But it crash library loading in ui application

I can't finish variant with dependOn because lack of knowledge about how cook it right :) May be you can advice something?

@lavcraft
Copy link
Author

For explanation

i have three files

/devices.ts
/contexts.ts
/call.ts

For the output i need one shared file - contexts.js , (because react hook useContext check similarity via reference).
And two separate files devices.js and call.js which reuse contexts.js. But now i have thre independent module - devices.js, call.js, contexts.js :)

@vankop
Copy link
Member

vankop commented Mar 31, 2022

but what a reason to create a separate entries? just use your app entrypoint

@lavcraft
Copy link
Author

@vankop main reason is libraries and code which not support SSR.

For example:
I can use useDevices hook in this component - https://github.com/lavcraft/react-hooks-composition-in-webpack-entry-problem-example/blob/master/packages/ui/src/pages/index.tsx#L20

But i can't use useCall. useCall depend on libraries who use window as global var. When import from external libraries like this it produce import fail. Component like this, should be decorated with dynamic import (example)

I want to save ability to use some hooks without dynamic imports.

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

No branches or pull requests

2 participants