Skip to content

Utilities

Overview

Utility functions provide various miscellaneous functionality for advanced use cases.

Pitfall

Any utility functions not documented here are not considered part of the public API and may change without notice.


Register Iframe

This function is used register a Django view as a ReactPy iframe.

It is mandatory to use this function alongside view_to_iframe.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.apps import AppConfig

from example import views
from reactpy_django.utils import register_iframe


class ExampleAppConfig(AppConfig):
    name = "example"

    def ready(self):
        register_iframe(views.hello_world)
See Interface

Parameters

Name Type Description Default
view Callable | View | str The view to register. Can be a function or class based view, or a dotted path to a view. N/A

Returns

None

Only use this within AppConfig.ready()

You should always call register_iframe within a Django AppConfig.ready() method. This ensures you will retain multiprocessing compatibility, such as with ASGI web server workers.


Register Component

This function is used register a root component with ReactPy.

Typically, this function is automatically called on all components contained within Django templates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from django.apps import AppConfig

from reactpy_django.utils import register_component


class ExampleAppConfig(AppConfig):
    name = "example"

    def ready(self):
        register_component("example_project.my_app.components.hello_world")
See Interface

Parameters

Name Type Description Default
component ComponentConstructor | str The component to register. Can be a component function or dotted path to a component. N/A

Returns

None

Only use this within MyAppConfig.ready()

You should always call register_component within a Django MyAppConfig.ready() method. This ensures you will retain multiprocessing compatibility, such as with ASGI web server workers.

Do I need to use this?

You typically will not need to use this function.

For security reasons, ReactPy requires all root components to be registered. However, all components contained within Django templates are automatically registered.

This function is commonly needed when you have configured your host to a dedicated Django rendering application that doesn't have templates.


Django Query Postprocessor

This is the default postprocessor for the use_query hook.

Since ReactPy is rendered within an asyncio loop, this postprocessor is exists to prevent Django's SynchronousOnlyException by recursively prefetching fields within Django's ORM. Note that this effectively eliminates Django's lazy execution behavior.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from reactpy import component

from example.models import TodoItem
from reactpy_django.hooks import use_query
from reactpy_django.utils import django_query_postprocessor


def get_items():
    return TodoItem.objects.all()


@component
def todo_list():
    # These postprocessor options are functionally equivalent to ReactPy-Django's default values
    item_query = use_query(
        get_items,
        postprocessor=django_query_postprocessor,
        postprocessor_kwargs={"many_to_many": True, "many_to_one": True},
    )

    return item_query.data
1
2
3
4
5
from django.db.models import CharField, Model


class TodoItem(Model):
    text: CharField = CharField(max_length=255)
See Interface

Parameters

Name Type Description Default
data QuerySet | Model The Model or QuerySet to recursively fetch fields from. N/A
many_to_many bool Whether or not to recursively fetch ManyToManyField relationships. True
many_to_one bool Whether or not to recursively fetch ForeignKey relationships. True

Returns

Type Description
QuerySet | Model The Model or QuerySet with all fields fetched.

Last update: December 2, 2024
Authors: Mark Bakhit