Skip to content

Click event for react polygon is not working #6138

@cognitiveRobot

Description

@cognitiveRobot

I tried with color picker example which is working.
But my try with react-leaflet is failed. Here is my attempt

import reflex as rx
from reflex.components.component import NoSSRComponent

multiPolygon = [
    [
        [51.51, -0.12],
        [51.51, -0.13],
        [51.53, -0.13],
    ],
    [
        [51.51, -0.05],
        [51.51, -0.07],
        [51.53, -0.07],
    ],
]


class MapContainer(rx.NoSSRComponent):
    library = "react-leaflet"

    tag = "MapContainer"

    center: rx.Var[list]

    zoom: rx.Var[int]

    scroll_wheel_zoom: rx.Var[bool]

    # Can also pass a url like: https://unpkg.com/leaflet/dist/leaflet.css
    def add_imports(self):
        return {"": ["leaflet/dist/leaflet.css"]}


class TileLayer(rx.NoSSRComponent):
    library = "react-leaflet"

    tag = "TileLayer"

    url: rx.Var[str]


class Polygon(rx.NoSSRComponent):
    library = "react-leaflet"

    tag = "Polygon"

    positions: rx.Var[list[list]]
    color: rx.Var[str] = "#ff0000"
    event_handlers: rx.EventHandler[lambda color: [color]] #handler name as suggessted
    eventHandlers: rx.EventHandler[lambda color: [color]]
    click: rx.EventHandler[lambda color: [color]]


map_container = MapContainer.create
tile_layer = TileLayer.create
polygon = Polygon.create

class ReactMapState(rx.State):
    color: str

    def set_color(self, color):
        print(color)


def react_map() -> rx.Component:
    return map_container(
        tile_layer(url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"),
        # polygon(positions=multiPolygon, event_handlers=ReactMapState.set_color),
        polygon(positions=multiPolygon, eventHandlers=ReactMapState.set_color),
        # polygon(positions=multiPolygon, click=ReactMapState.set_color),
        center=[51.505, -0.09],
        zoom=13,
        # scroll_wheel_zoom=True
        width="100%",
        height="50vh",
    )

When I use event_handlers as the attribute name I get the following error:

[Reflex Frontend Exception]
 TypeError: str.replace is not a function
TypeError: str.replace is not a function
    at trim (http://..../node_modules/.vite/deps/react-leaflet.js?v=58ce41a2:217:39)
    at splitWords (http://..../node_modules/.vite/deps/react-leaflet.js?v=58ce41a2:220:11)
    at NewClass.on (http://..../node_modules/.vite/deps/react-leaflet.js?v=58ce41a2:359:14)
    at addEventHandlers (http://..../node_modules/.vite/deps/react-leaflet.js?v=58ce41a2:115:47)
    at Object.react_stack_bottom_frame (http://..../node_modules/.vite/deps/react-dom_client.js?v=58ce41a2:12889:13)
    at runWithFiberInDEV (http://..../node_modules/.vite/deps/react-dom_client.js?v=58ce41a2:836:66)
    at commitHookEffectListMount (http://..../node_modules/.vite/deps/react-dom_client.js?v=58ce41a2:6602:153)
    at commitHookPassiveMountEffects (http://..../node_modules/.vite/deps/react-dom_client.js?v=58ce41a2:6637:55)
    at reconnectPassiveEffects (http://...../node_modules/.vite/deps/react-dom_client.js?v=58ce41a2:7686:6)
    at doubleInvokeEffectsOnFiber (http://..../node_modules/.vite/deps/react-dom_client.js?v=58ce41a2:8909:127)

What am I missing? Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions