Skip to content

Commit 90bb060

Browse files
authored
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
1 parent e78f95c commit 90bb060

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

docs/wrapping-react/local-packages.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,67 @@ class Hello(rx.Component):
3838

3939
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.
4040

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:
4442

4543
```javascript
46-
// /path/to/components/hello.js
44+
// /path/to/components/hello.jsx
4745
import React from 'react';
4846

49-
export function Hello() {
47+
export function Hello({name, onGreet}) {
5048
return (
5149
<div>
52-
<h1>Hello!</h1>
50+
<h1>Hello, {name}!</h1>
51+
<button onClick={() => onGreet(name)}>Greet</button>
5352
</div>
5453
)
5554
}
5655
```
5756

58-
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`.
5960

6061
```python
6162
import reflex as rx
6263

63-
hello_path = rx.asset("hello.js", shared=True)
64-
public_hello_path = "$/public/" + hello_path
64+
hello_path = rx.asset("./hello.jsx", shared=True)
65+
hello_css_path = rx.asset("./hello.css", shared=True)
6566

6667
class Hello(rx.Component):
6768
# Use an absolute path starting with $/public
68-
library = public_hello_path
69+
library = f"$/public{hello_path}"
6970

7071
# Define everything else as normal.
7172
tag = "Hello"
73+
74+
name: rx.Var[str] = rx.Var("World")
75+
on_greet: rx.EventHandler[rx.event.passthrough_event_spec(str)]
76+
77+
# Include any related CSS files with rx.asset to ensure they are copied.
78+
def add_imports(self):
79+
return {"": f"$/public/{hello_css_path}"}
7280
```
7381

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+
74102
# Local Packages
75103

76104
If the component is part of a local package, available on Github, or

0 commit comments

Comments
 (0)