浏览代码

Check if router is ready on page load (#241)

Thomas Brandého 2 年之前
父节点
当前提交
f370642205
共有 2 个文件被更改,包括 5 次插入1 次删除
  1. 3 0
      pynecone/compiler/templates.py
  2. 2 1
      pynecone/compiler/utils.py

+ 3 - 0
pynecone/compiler/templates.py

@@ -158,9 +158,12 @@ SOCKET = constants.SOCKET
 STATE = constants.STATE
 EVENTS = constants.EVENTS
 SET_RESULT = format_state_setter(RESULT)
+READY = f"const {{ isReady }} = {ROUTER};"
 USE_EFFECT = join(
     [
         "useEffect(() => {{",
+        "  if(!isReady)",
+        f"    return;",
         f"  if (!{SOCKET}.current) {{{{",
         f"    connect({SOCKET}, {{state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})",
         "  }}",

+ 2 - 1
pynecone/compiler/utils.py

@@ -119,7 +119,8 @@ def compile_state(state: Type[State]) -> str:
     )
     router = templates.ROUTER
     socket = templates.SOCKET
-    return templates.join([synced_state, result, router, socket])
+    ready = templates.READY
+    return templates.join([synced_state, result, router, socket, ready])
 
 
 def compile_events(state: Type[State]) -> str: