Skip to content

Interact with component from outside #531

Answered by Hammer2900
Hammer2900 asked this question in Question
Discussion options

You must be logged in to vote

If I have understood correctly, this is the right way to interact.

import idom
from idom.server.sanic import SharedClientStateServer
from sanic import Sanic
from sanic.response import json

app = Sanic()

count, set_count = idom.Ref(), idom.Ref()


@idom.component
def ClickCountNew():
    count.current, set_count.current = idom.hooks.use_state(0)
    return idom.html.button(
        {'onClick': lambda event: set_count.current(count.current + 1)},
        [f'Click count: {count}'],
    )


@app.route('/set')
async def test(request):
    set_count.current(100)
    return json({'hello': True})


per_client_state = SharedClientStateServer(ClickCountNew, app=app)

per_client_state.run('localhost'

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@rmorshea
Comment options

@Hammer2900
Comment options

@rmorshea
Comment options

@Hammer2900
Comment options

Answer selected by Archmonger
@rmorshea
Comment options

@Hammer2900
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants