question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React hooks composition fail when packaging into different entries

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vankopcommented, Mar 31, 2022

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

1reaction
vankopcommented, 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 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug: React hooks composition fail when packaging into ...
Configure webpack with two entries - /src/usr-hook1 and /src/usr-hook2` for some reason; Import in main application and use my hooks like this.
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
Hooks FAQ - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >
Best Practices With React Hooks - Smashing Magazine
This article covers the rules of React Hooks and how to effectively start using them in your projects. Please note that in order...
Read more >
How To Manage State with Hooks on React Components
React exports several Hooks that you can import directly from the main React package. By convention, React Hooks start with the word use ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found