You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Local Component section of Wrapping React (#1584)
* Fix the existing broken example due to using JSX syntax in a JS file.
* Add more explanation about using rx.asset
* Add example of including CSS import
* Add important consideration and usecase bullets
Copy file name to clipboardExpand all lines: docs/wrapping-react/local-packages.md
+38-10Lines changed: 38 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,39 +38,67 @@ class Hello(rx.Component):
38
38
39
39
You can also wrap components that you have written yourself. For local components (when the code source is directly in the project), we recommend putting it beside the files that is wrapping it.
40
40
41
-
You can then use the path obtained via `rx.asset` to reference the component path.
42
-
43
-
So if you create a file called `hello.js` in the same directory as the component with this content:
41
+
If there is a file `hello.jsx` in the same directory as the component with this content:
You can specify the library as follow (note: we use the `public` directory here instead of `assets` as this is the directory that is served by the web server):
57
+
The python app can use the `rx.asset` helper to copy the component source into
58
+
the generated frontend, after which the `library` path in the `rx.Component` may
59
+
be specified by prefixing `$/public` to that path returned by `rx.asset`.
# Include any related CSS files with rx.asset to ensure they are copied.
78
+
defadd_imports(self):
79
+
return {"": f"$/public/{hello_css_path}"}
72
80
```
73
81
82
+
## Considerations
83
+
84
+
When wrapping local components, keep the following in mind:
85
+
86
+
1.**File Extensions**: Ensure that the file extensions are correct (e.g., `.jsx` for React components and `.tsx` for TypeScript components).
87
+
2.**Asset Management**: Use `rx.asset` with `shared=True` to manage any assets (e.g., images, styles) that the component depends on.
88
+
3.**Event Handling**: Define any event handlers (e.g., `on_greet`) as part of the component's API and pass those to the component _from the Reflex app_. Do not attempt to hook into Reflex's event system directly from Javascript.
89
+
90
+
## Use Case
91
+
92
+
Local components are useful when shimming small pieces of functionality that are
93
+
simpler or more performant when implemented directly in Javascript, such as:
94
+
95
+
* Spammy events: keys, touch, mouse, scroll -- these are often better processed on the client side.
96
+
* Using canvas, graphics or WebGPU
97
+
* Working with other Web APIs like storage, screen capture, audio/midi
98
+
* Integrating with complex third-party libraries
99
+
* For application-specific use, it may be easier to wrap a local component that
100
+
provides the needed subset of the library's functionality in a simpler API for use in Reflex.
101
+
74
102
# Local Packages
75
103
76
104
If the component is part of a local package, available on Github, or
0 commit comments