tsdx: Can't use hooks when `react` & `react-dom` are dev dependencies
Current Behavior
Thank you for this project!
I’m not sure if I missed or didn’t understand something, but when choosing the react template, react & react-dom are added as dev dependencies. When you then try to use a hook inside your library, you get a react-invalid-hook-call error. It works fine when removing them as dev dependencies and running yarn again.
Example:
export const Thing = () => {
const [fooBar, setFooBar] = React.useState('foo');
return (
<div onClick={() => setFooBar('bar')}>
the snozzberries taste like {fooBar}
</div>
);
};
Expected behavior
I would expect the template to enable me to use hooks.
Suggested solution(s)
No idea if “just removing the dev dependencies” is the proper solution. It at least worked for me.
Your environment
| Software | Version(s) |
|---|---|
| TSDX | 0.5.4 |
| TypeScript | 3.4.5 |
| Browser | Chrome 74.0.3729.108 |
| npm/Yarn | Yarn 1.12.3 |
| Operating System | MacOS 10.14.4 |
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 16 (4 by maintainers)
Commits related to this issue
- use parcel's aliasing for react template example deps use parcel's aliasing for react template example deps. closes https://github.com/palmerhq/tsdx/issues/64 — committed to jaredpalmer/tsdx by swyxio 5 years ago
- use parcel's aliasing for react template example deps (#88) use parcel's aliasing for react template example deps. closes https://github.com/palmerhq/tsdx/issues/64 — committed to jaredpalmer/tsdx by swyxio 5 years ago
- Initial commit — committed to jhmoraes/books-tdsx by jhmoraes a year ago
I think I found the problem. I was testing it using
yarn linkand I couldn’t get it to work without removing thedevDependency. Publishing a package based on0.5.5and using that worked fine. Thanks you all!This doesn’t work if you’re trying to deploy the example site to Netlify or something similar. Is there any other way to accomplish this?
made a PR to do just that
I think the solution I like best now is to use Parcel’s alias like so:
Facing the same problem; I don’t think it is caused by tsdx itself - hooks are quite picky when multiple react instances are present - see https://github.com/facebook/react/issues/14257. Tried doing the recommendations from there; Linked the package react using
yarn linkand added"react": "link:../node_modules/react"to package.json. Did not work for me.The solution which worked for me:
exampleparcelas dev dependency in the package root.example/index.tsxto 'src/index.tsx`.parcel example/index.htmlProbably not perfect, but at least it works quite fast.
peerdeps in v0.5.5 https://github.com/palmerhq/tsdx/commit/9f5c6de9f8c7472fcd0dbbfe15aa18434e742d9f