Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

How to add a component with event handler? #2

Closed
fsmosca opened this issue Aug 29, 2023 · 2 comments
Closed

How to add a component with event handler? #2

fsmosca opened this issue Aug 29, 2023 · 2 comments

Comments

@fsmosca
Copy link

fsmosca commented Aug 29, 2023

I created an app menu item for a sample counter and temperature components that I would like to add. I clone the repo and have not use the rf-init command. I am on windows 10.

config.py

"navigation": {
    "home": {
        "getting started": "start.py"
    },
    "app": {
        "introduction": "intro.py",
        "temperature": "temperature.py",
        "counter": "counter.py",
    }
},

image

The counter.py has this entry.

# Main content area: takes in rx.Componenets and passes them to base file
def __components__(self):
    return [
        # add your components below #
        CounterComponent()
        # end your components above #
    ]

That CounterComponent() is an import from my content folder.

from content.counter import CounterComponent

The content folder is under the reflexify folder.

image

My content/counter.py

from app.states.mainState import MainState
import reflex as rx


class CounterState(MainState):
    count: int = 0

    def increment(self):
        self.count += 1

    def decrement(self):
        self.count -= 1


def CounterComponent():
    return rx.box(
        rx.text('Counter App', font_size='1.5em'),
        rx.spacer(),
        rx.hstack(
            rx.button(rx.icon(tag='add'), color='green',
                      on_click=CounterState.increment
            ),
            rx.text(CounterState.count),
            rx.button(rx.icon(tag='minus'), color='red',
                      on_click=CounterState.decrement
            )
        )
    )

What is the issue?

The issue is that when you press the plus or minus button, the counter is unresponsive, meaning if you press the plus sign, the counter shown will not change, similar to minus sign.

image

@LineIndent
Copy link
Owner

I tested this on my end and it worked:

Screenshot 2023-08-29 at 9 27 39 PM

This is what I did:

  1. In the file where you want to put the counter:
from app.core.base import RxBasePage
from app.helpers.nav_helpers import NavHelper
import reflex as rx
from app.states.counterState import CounterState

def CounterComponent():
    return rx.box(
        rx.text("Counter App", font_size="1.5em"),
        rx.spacer(),
        rx.hstack(
            rx.button(
                rx.icon(tag="add"), color="green", on_click=CounterState.increment
            ),
            rx.text(f"{CounterState.count}"),
            rx.button(
                rx.icon(tag="minus"), color="red", on_click=CounterState.decrement
            ),
        ),
    )


class RxPage:
    # Title of page: must match high-level key in config.py
    def __title__(self):
        return "Reflexfy"

    # Page route path: must follow /parent-key/file-name *without .py extension*
    def __route__(self):
        return "/"

    # Left navigation panel: automated based on config navigation order
    def __left_navigation__(self):
        nav: list = NavHelper.__get_left_navigation__(self.__title__())
        return NavHelper.__set_left_navigation__(nav)

    # Right navigation panel: TBD
    def __right__navigation__(self):
        return []

    # Mobile navigation drop down
    def __mobile_navigation__(self):
        return NavHelper.__get_left_navigation__(self.__title__())

    # Main content area: takes in rx.Componenets and passes them to base file
    def __components__(self):
        return [CounterComponent()]

    # Build method: creates a new instance for the page above
    def build(self):
        page = RxBasePage(
            self.__components__(),
            self.__left_navigation__(),
            self.__right__navigation__(),
            self.__mobile_navigation__(),
        )
        return page.build()
  1. Place your states all in the states folder - it'll be easier to organize and keep track of. Inside of states i created a file called counterState.py and placed the following code as such:
from .mainState import MainState

class CounterState(MainState):
    count: int = 0

    def increment(self):
        self.count += 1

    def decrement(self):
        self.count -= 1

This i what I did and the counter worked.
Don't place your own components, like the counter, outside the app folder. This messes with the path, even if you're using Reflex plainly, always place the files inside the main directory. So for example, your content folder, place it inside the app folder and this way you'll have access to it easily.

@fsmosca
Copy link
Author

fsmosca commented Aug 30, 2023

Thanks it worked.

@fsmosca fsmosca closed this as completed Aug 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants