reflex/reflex/.templates/jinja/web/pages/index.js.jinja2
2023-08-15 15:17:08 -07:00

59 lines
1.4 KiB
Django/Jinja

{% extends "web/pages/base_page.js.jinja2" %}
{% block declaration %}
{% for custom_code in custom_codes %}
{{custom_code}}
{% endfor %}
{% endblock %}
{% block export %}
export default function Component() {
const {{const.router}} = useRouter()
const { {{const.color_mode}}, {{const.toggle_color_mode}} } = {{const.use_color_mode}}()
const focusRef = useRef();
// Function to add new files to be uploaded.
const File = files => {{state_name|react_setter}}(state => ({
...state,
files,
}))
// Main event loop.
const [{{state_name}}, Event, notConnected] = useEventLoop(
{{initial_state|json_dumps}},
[E('{{state_name}}.{{const.hydrate}}', {})],
)
// Set focus to the specified element.
useEffect(() => {
if (focusRef.current) {
focusRef.current.focus();
}
})
{% if is_dynamic %}
// Route after the initial page hydration.
useEffect(() => {
const change_complete = () => Event([E('{{state_name}}.{{const.hydrate}}', {})])
{{const.router}}.events.on('routeChangeComplete', change_complete)
return () => {
{{const.router}}.events.off('routeChangeComplete', change_complete)
}
}, [{{const.router}}])
{% endif %}
{% for hook in hooks %}
{{ hook }}
{% endfor %}
return (
<Fragment>
{%- if err_comp -%}
{{ utils.render(err_comp, indent_width=1) }}
{%- endif -%}
{{utils.render(render, indent_width=0)}}
</Fragment>
)
}
{% endblock %}