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
Copy file name to clipboardExpand all lines: docs/wrapping-react/library-and-tags.md
+69Lines changed: 69 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,8 @@ This is when we will define the most important attributes of the component:
27
27
4.**lib_dependencies**: Any additional libraries needed to use the component.
28
28
5.**is_default**: (Optional) If the component is a default export from the module, set this to `True`. Default is `False`.
29
29
30
+
Optionally, you can override the default component creation behavior by implementing the `create` class method. Most components won't need this when props are straightforward conversions from Python to JavaScript. However, this is useful when you need to add custom initialization logic, transform props, or handle special cases when the component is created.
31
+
30
32
```md alert warning
31
33
# When setting the `library` attribute, it is recommended to included a pinned version of the package. Doing so, the package will only change when you intentionally update the version, avoid unexpected breaking changes.
32
34
```
@@ -49,6 +51,21 @@ class MyBaseComponent(rx.Component):
49
51
50
52
# If the component is a default export from the module, set this to True.
51
53
is_default =True/False
54
+
55
+
@classmethod
56
+
defcreate(cls, *children, **props):
57
+
"""Create an instance of MyBaseComponent.
58
+
59
+
Args:
60
+
*children: The children of the component.
61
+
**props: The props of the component.
62
+
63
+
Returns:
64
+
The component instance.
65
+
"""
66
+
# Your custom creation logic here
67
+
returnsuper().create(*children, **props)
68
+
52
69
```
53
70
54
71
# Wrapping a Dynamic Component
@@ -95,3 +112,55 @@ The reason for this is that it does not make sense for your server to render the
95
112
96
113
In addition, if in the component documentation it mentions nextJS compatibility or server side rendering compatibility, it is a good sign that it requires dynamic imports.
97
114
115
+
# Advanced - Parsing a state Var with a JS Function
116
+
When wrapping a component, you may need to parse a state var by applying a JS function to it.
117
+
118
+
## Define the parsing function
119
+
120
+
First you need to define the parsing function by writing it in `add_custom_code`.
121
+
122
+
```python
123
+
124
+
defadd_custom_code(self) -> list[str]:
125
+
"""Add custom code to the component."""
126
+
# Define the parsing function
127
+
return [
128
+
"""
129
+
function myParsingFunction(inputProp) {
130
+
// Your parsing logic here
131
+
return parsedProp;
132
+
}"""
133
+
]
134
+
```
135
+
136
+
## Apply the parsing function to your props
137
+
138
+
Then, you can apply the parsing function to your props in the `create` method.
139
+
140
+
```python
141
+
from reflex.vars.base import Var
142
+
from reflex.vars.function import FunctionStringVar
143
+
144
+
...
145
+
@classmethod
146
+
defcreate(cls, *children, **props):
147
+
"""Create an instance of MyBaseComponent.
148
+
149
+
Args:
150
+
*children: The children of the component.
151
+
**props: The props of the component.
152
+
153
+
Returns:
154
+
The component instance.
155
+
"""
156
+
# Apply the parsing function to the props
157
+
if (prop_to_parse := props.get("propsToParse")) isnotNone:
Copy file name to clipboardExpand all lines: docs/wrapping-react/props.md
+38-5Lines changed: 38 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ Broadly, there are three kinds of props you can encounter when wrapping a React
17
17
18
18
Simple props are the most common type of props you will encounter when wrapping a React component. They are passed directly to the component and can be of any type (but most commonly strings, numbers, booleans, and structures).
19
19
20
+
For custom types, you can use `TypedDict` to define the structure of the custom types. However, if you need the attributes to be automatically converted to camelCase once compiled in JS, you can use `rx.PropsBase` instead of `TypedDict`.
21
+
20
22
```python
21
23
classCustomReactType(TypedDict):
22
24
"""Custom React type."""
@@ -26,6 +28,15 @@ class CustomReactType(TypedDict):
26
28
attribute2: bool
27
29
attribute3: int
28
30
31
+
32
+
classCustomReactType2(rx.PropsBase):
33
+
"""Custom React type."""
34
+
35
+
# Define the structure of the custom type to match the Javascript structure.
36
+
attr_foo: str# will be attrFoo in JS
37
+
attr_bar: bool# will be attrBar in JS
38
+
attr_baz: int# will be attrBaz in JS
39
+
29
40
classSimplePropsComponent(MyBaseComponent):
30
41
"""MyComponent."""
31
42
@@ -46,9 +57,12 @@ class SimplePropsComponent(MyBaseComponent):
46
57
# props annotated as `CustomReactType` in javascript
47
58
props5: rx.Var[CustomReactType]
48
59
60
+
# props annotated as `CustomReactType2` in javascript
61
+
props6: rx.Var[CustomReactType2]
62
+
49
63
# Sometimes a props will accept multiple types. You can use `|` to specify the types.
50
64
# props annotated as `string | boolean` in javascript
51
-
props6: rx.Var[str|bool]
65
+
props7: rx.Var[str|bool]
52
66
```
53
67
54
68
## Callback Props
@@ -92,14 +106,30 @@ class InputEventType(TypedDict):
0 commit comments