-
Notifications
You must be signed in to change notification settings - Fork 15
/
example.html
83 lines (71 loc) · 2.66 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>React Hooks Example</title>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- <script src="https://unpkg.com/blx-react-hooks@0.0.1/react-hooks.min.js"></script> -->
<script src="./dist/react-hooks.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const {
withHooks,
useState,
useContext,
useReducer,
useCallback,
useMemo,
useRef,
useEffect,
useLayoutEffect,
useImperativeMethods
} = ReactHooks
const appContainer = document.querySelector('#app');
const countContext = React.createContext(0)
const Increase = withHooks(function Increase({ dispatch }, ref){
window.ref = ref
const count = useContext(countContext)
useImperativeMethods(ref, () => {
return {
['hello'+count]: () => {console.log('hello' + count)}
}
}, [count])
return <button onClick={() => dispatch({type: 'increase'})}> + </button>
})
const Decrease = withHooks(function Decrease({ dispatch }){
const count = useContext(countContext)
return <button onClick={() => dispatch({type: 'decrease'})}> - </button>
})
const reducer = (count, {type}) => {
if (type === 'increase') {
return count + 1
} else if (type === 'decrease') {
return count - 1
}
throw new Error()
}
const App = withHooks(function App({}){
const [count, dispatch] = useReducer(reducer,0)
const ref = useRef()
window.ref = ref
return (
<countContext.Provider value={count}>
<div ref={ref} style={{ textAlign: 'center' }}>
<h1>React Hooks Example</h1>
<Increase dispatch={dispatch}/>
| Count: { count } |
<Decrease dispatch={dispatch}/>
</div>
</countContext.Provider>
)
})
ReactDOM.render(<App/>, appContainer);
</script>
</body>
</html>