-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
Compatibility with preact #588
Comments
Thanks for your interest! Rest Hooks imports createContext from React itself (listed as a peerDep). Seems like you would need to tell your build system to change all react imports to preact imports? Would love addition of a unit test as well as docs instructions for usage with preact. I'm happy to fix any reasonable compatibility to pass said test. |
Build system I'm using (WMR/ Rollup) is already configured to alias I'm using SWR in same setup without any issues even that it's also using react as peer dependency. Issue must come from somewhere else. |
I downloaded a zip of the codesandbox, when i run ./~/@rest-hooks/core/lib/react-integration/hooks/useResetter.js
7:17-27 "export 'useContext' was not found in 'react'
./~/@rest-hooks/core/lib/react-integration/hooks/useResetter.js
8:24-35 "export 'useCallback' was not found in 'react'
./~/@rest-hooks/core/lib/react-integration/hooks/useFetchDispatcher.js
9:17-27 "export 'useContext' was not found in 'react'
./~/@rest-hooks/core/lib/react-integration/hooks/useFetchDispatcher.js
10:24-35 "export 'useCallback' was not found in 'react'
./~/@rest-hooks/core/lib/react-integration/hooks/useInvalidateDispatcher.js
7:17-27 "export 'useContext' was not found in 'react'
./~/@rest-hooks/core/lib/react-integration/hooks/useInvalidateDispatcher.js
8:29-40 "export 'useCallback' was not found in 'react' Is there something weird with the tooling maybe? |
I don't find |
According to https://www.npmjs.com/package/preact-compat - it's only for outdated versions of preact, which is probably why it doesn't have hooks.
The config in codesandbox has:
|
yeah, they are: https://preactjs.com/guide/v10/hooks#usecontext Maybe this would be helpful: https://preactjs.com/guide/v10/switching-to-preact |
Right, but preact-compat is version 8, you need to have |
Ah, seems that the codesandbox template ships with outdated packages |
Got the codesandbox to work by doing two things:
'root' wasn't actually on the page |
Yeah, I've done the same in meantime and it does build and render now. I'll come back to that tomorrow. |
Gonna close this for now since it seems to work. Would love any docs contributions on getting start with the library in Preact! :) |
I'm sorry, the issue is not resolved. Compatiblity with Preact may be achieved only in projects created with preact-cli (I assume it's compiling vendor packages with own babel config and does other tweaks). I've prepared issue reproduction project here: preact-rollup-react-jsx-transforms. Basically there are two issues:
|
It’s quite secure to rely on node globals - a node module bundler that does not provide them is broken. |
@ljharb |
This comment has been minimized.
This comment has been minimized.
node doesn't have window. I believe globalThis is now the canonical way of referring to such things, so perhaps this should change. This would break older integrations however as globalThis is somewhat new. @piotr-cz can you open a new issue to track these new things? Also I'm a bit confused about which usage of the jsx transform will work with preact. I'm hoping it's the new version as I hope to use that in the future. |
You can use https://npmjs.com/globalThis to maintain support, but indeed, global is the proper thing to use, and webpack 5 has intentionally broken its core use case by not doing this by default. |
@ljharb How might I include the https://www.npmjs.com/package/globalthis shim only for legacy cases, as to not bloat code size? |
I suppose you could do a conditional require, but generally speaking you can't avoid code size increases when maintaining support for older versions of things. In this case, |
Hmm, does |
If it's a global variable there, it should. The only reason I can think of to use |
Ah, I now realize it's because microsoft/TypeScript#40807. I can just add the libdef myself so this won't be a problem. |
@piotr-cz When using rollup you can define process via https://github.com/coinbase/rest-hooks/blob/master/packages/rest-hooks/rollup.config.js#L44 Though I don't think rollup is intended for actual projects, but just to build libraries. |
@ntucker For handling globals, it's fairly easy to replace them as you noted. Actually I'm using Rollup as a web app bundler for few years. |
React version (preact 10.5.12)
Concurrent mode no
Rest Hooks version (e.g., 5.0.6)
Describe the bug (This is rather a feature request but IMO it's more appropriate to use bug template)
This package seems to not work with Preact (alternative to React with compatible API).
Problem comes up when wrapping an application with
<CacheProvider>
.Engine stops rendering any children and what's unfortunate doesn't show any errors.
Small hint is that ot does render when I remove context providers and keep just
{children}
inhttps://github.com/coinbase/rest-hooks/blob/e1e353dfc64725c79ab99bb6a0c85114399c6dfc/packages/core/src/react-integration/provider/CacheProvider.tsx#L56-L64
To Reproduce
Steps to reproduce the behavior:
@rest-hooks/rest
&rest-hooks
to dependenciesimport { CacheProvider } from 'rest-hooks';
to src/index.jsOr check codesandbox example
Expected behavior
Package works with Preact just as it would with React
Additional context
No console errors.
The text was updated successfully, but these errors were encountered: