Skip to content

Commit 272924c

Browse files
authored
[Docs] Added a gotcha when importing in a non-react project (#1089)
* [docs] added a gotcha for importing when not using react
1 parent 4f2f6e3 commit 272924c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

docs/how-tos/some-gotchas.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,27 @@ If the workaround is not applied and you are using react with [devtools()](https
224224
As a result, the devtools will not display any state change.
225225

226226
Additionally, this issue involved not only updating devtools, but also triggering `re-render`.
227+
228+
## Issue with imports when using a library other than `react` (i.e. solidjs)
229+
230+
Valtio does not have to work within react, however it was built with react in mind. This being the case, the main `valtio` module exports the react modules alongside the vanilla modules for convenience. This means if you are attempting to import from the main `valtio` module or the `valtio/utils` submodule into a non-react project, you may end up with build errors like this:
231+
232+
```
233+
node_modules/.pnpm/valtio@2.1.4/node_modules/valtio/esm/react.mjs (2:18): "useRef" is not exported by "__vite-optional-peer-dep:react:valtio", imported by "node_modules/.pnpm/valtio@2.1.4/node_modules/valtio/esm/react.mjs".
234+
```
235+
236+
This occurs because the main valtio module exports both framework-agnostic and React-specific functionality, causing build tools like Rollup to look for React dependencies even when they're not needed.
237+
238+
There is a simple fix for this, however. Instead of importing from the main `valtio` module like this:
239+
240+
```ts
241+
import { proxy, snapshot, subscribe } from 'valtio'
242+
```
243+
244+
you can import directly from the framework-agnostic `vanilla` submodule:
245+
246+
```ts
247+
import { proxy, snapshot, subsribe } from 'valtio/vanilla'
248+
// this also applies for the utils
249+
import { proxyMap, deepClone } from 'valtio/vanilla/utils'
250+
```

0 commit comments

Comments
 (0)