Problem Description:
I have created an advanced, interactive feedback form that:
- Uses custom React components for enhanced user experience
- Implements animated radio button inputs styled as emotive faces
- Provides dynamic, interactive feedback visualization
- Requires custom styling and animations beyond standard Reflex form capabilities
What’s Working:
- Props data flow is functioning perfectly - demonstrated by
props.message
successfully passing data between Reflex and my custom React component (from reflex backend to the own react component) - Custom styling and animations are rendering as intended
Technicalities:
- A form with radio button inputs
- The form is defined in a custom JS file (
hello.js
) and usesprops.on_submit
for form submission - On the Reflex side, I’ve created a custom component class (
Hello
) that:
- Defines message and on_submit props
- Successfully passes the message prop to React
- However, the form submission event handler is not being triggered when the submit button is clicked
- The WebSocket connection opens and closes, but the event handler doesn’t execute
** The core technical challenge is:**
- While props.message works flawlessly showing data flow is possible, the
props.on_submit
event is not triggering the backend event handler - Need to properly connect this custom React form component with Reflex’s event handling system
(* Maintain all the custom styling and interactivity that makes this feedback form competitive and engaging*) - this works up to now
The core issue is understanding how to properly connect a custom component’s form submission event in React with Reflex’s event handling system.
** The hello.js
file:**
// assets/hello.js
import React from 'react';
export function Hello(props) {
return (
<>
{/* Added a dedicated div for my message streaming from reflex backend */}
<div className="message-container">
<h1>{props.message}</h1>
</div>
<div className="background-layer"></div>
<form onSubmit={props.on_submit}>
<div className="feedback">
<label className="angry">
<input type="radio" defaultValue={1} name="feedback" />
<div>
{/* Additional content goes here */}
</div>
</label>
{/* Adding other radio buttons here */}
{/* Submit button */}
<button type="submit">Submit</button>
</div>
</form>
</>
);
}
main component in my main reflex .py app:
class Hello(rx.Component):
# Using an absolute path starting with /public
library = "/public/hello"
# Defining everything else as normal.
tag = "Hello"
#Props
message: rx.Var[int]
on_submit: rx.EventHandler[lambda form_data: [form_data]] # Specifying to take one argument
**rx State **:
value:int = 0
class FormState(rx.State):
@rx.event
def handle_value(self, form_data: dict):
"""Handle the form submit"""
print(f"the submitted value: {form_data["feedback"]}")
self.value = form_data["feedback"]
** final render**:
return rx.box(
Hello.create(
message=0, #this would be place holder to show the FormState.value
on_submit = FormState.handle_value
))
demo example:
Any help is appreciated