Skip to content

Commit c93996f

Browse files
committed
Introduced ord.reactjs package.
1 parent 231c9d9 commit c93996f

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

transcrypt/demos/react_demo/react_demo.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# Helper functions
1+
from org.reactjs import createElement, useState, useEffect, useRef
2+
from org.reactjs.dom import render as react_render
3+
24

5+
# Helper functions
36

47
def h(elm_type, props='', *args):
5-
return React.createElement(elm_type, props, *args)
8+
return createElement(elm_type, props, *args)
69

710

811
def render(react_element, destination_id, callback=lambda: None):
912
container = document.getElementById(destination_id)
10-
ReactDOM.render(react_element, container, callback)
11-
12-
13-
useState = React.useState
14-
useEffect = React.useEffect
15-
useRef = React.useRef
13+
react_render(react_element, container, callback)
1614

1715

1816
def useInterval(func, delay=None):
@@ -25,11 +23,11 @@ def useInterval(func, delay=None):
2523
ref = useRef(func)
2624
ref.current = func
2725

26+
@useEffect.withDeps(delay)
2827
def setup():
2928
id = setInterval(lambda: ref.current(), delay)
3029
return lambda: clearInterval(id)
3130

32-
useEffect(setup, [delay])
3331
return func
3432

3533

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
createElement = React.createElement
2+
createContext = React.createContext
3+
forwardRef = React.forwardRef
4+
Component = ReactComponent = React.Component
5+
6+
7+
useState = React.useState
8+
useEffect = React.useEffect
9+
useContext = React.useContext
10+
11+
useReducer = React.useReducer
12+
useCallback = React.useCallback
13+
useMemo = React.useMemo
14+
useRef = React.useRef
15+
useImperativeHandle = React.useImperativeHandle
16+
useLayoutEffect = React.useLayoutEffect
17+
useDebugValue = React.useDebugValue
18+
19+
20+
def withDeps(*deps):
21+
useHook = this
22+
def decorator(fn):
23+
useHook(fn, deps)
24+
return fn
25+
return decorator
26+
27+
28+
useEffect.withDeps = withDeps
29+
useLayoutEffect.withDeps = withDeps
30+
31+
32+
def useCallbackWithDeps(*deps):
33+
def decorator(fn):
34+
return React.useCallback(fn, deps)
35+
return decorator
36+
37+
useCallback.withDeps = useCallbackWithDeps
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
render = ReactDOM.render

0 commit comments

Comments
 (0)