React hooks composition fail when packaging into different entries
See original GitHub issueBug report
What is the current behavior?
React hook useContext(Context)
doesn’t work when call from another hook from different entry
For example:
- I have
useHook1
anduseHook2
.useHook2
calluseHook1
inside - 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
const Component: React.FC = () => {
const devices = useHook1();
const call = useHook2();
return null
}
export withHook1(withHook2(Component));
Result:
useHook1
- return right value in ComponentuseHook1
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
- Clone project
npm i
npm run all:start
ornpm run sdk:dev
and thennpm run ui:dev
in different terminal- 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:
- Created a year ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
but what a reason to create a separate entries? just use your app entrypoint
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 )