Skip to content

can you give an example how to use use_context #995

Answered by rmorshea
bobwatcherx asked this question in Question
Discussion options

You must be logged in to vote

Contexts allow you to pass values between components without doing so directly.

from reactpy import create_context, component, html, use_context, run

my_context = create_context(None)

@component
def example():
    return my_context(parent(), value=123)

@component
def parent():
    return child()

@component
def child():
    ctx_value = use_context(my_context)
    assert ctx_value == 123
    return html.h1(ctx_value)

run(example)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by rmorshea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants