4949
5050 // 从全局变量中安全获取函数,避免重新声明
5151 // 注意:不再使用解构赋值,避免变量声明冲突
52- const myRender = preact . render ;
53- const myH = preact . h ;
54- const myUseState = preactHooks . useState ;
55- const myUseEffect = preactHooks . useEffect ;
56- const myHtml = htm . bind ( myH ) ; // 使用myH而不是h
52+ const render = preact . render ;
53+ // const myH = preact.h;
54+ const useState = preactHooks . useState ;
55+ const useEffect = preactHooks . useEffect ;
56+ const html = htm . bind ( preact . h ) ; // 使用myH而不是h
5757
5858 function Counter ( ) {
59- const [ count , setCount ] = myUseState ( null ) ;
60- const [ loading , setLoading ] = myUseState ( true ) ;
61- const [ updating , setUpdating ] = myUseState ( false ) ;
62- const [ error , setError ] = myUseState ( null ) ;
59+ const [ count , setCount ] = useState ( null ) ;
60+ const [ loading , setLoading ] = useState ( true ) ;
61+ const [ updating , setUpdating ] = useState ( false ) ;
62+ const [ error , setError ] = useState ( null ) ;
6363
6464 // 从Python获取计数
6565 const fetchCount = async ( ) => {
7777 } ;
7878
7979 // 初始加载
80- myUseEffect ( ( ) => {
80+ useEffect ( ( ) => {
8181 fetchCount ( ) ;
8282 } , [ ] ) ;
8383
113113
114114 // 显示加载状态
115115 if ( loading ) {
116- return myHtml `<div>加载中...</div>` ;
116+ return html `< div > 加载中...</ div > ` ;
117117 }
118118
119- return myHtml `
119+ return html `
120120 < div class =${ updating ? 'loading' : '' } >
121121 < h1 > Preact计数器示例</ h1 >
122122 < p > 通过Python后端管理状态(完全离线)</ p >
123123
124124 < div class ="counter "> ${ count !== null ? count : '...' } </ div >
125125
126- ${ error && myHtml `<div class="error">${ error } </div>` }
126+ ${ error && html `< div class ="error "> ${ error } </ div > ` }
127127
128128 < div >
129129 < button
@@ -144,7 +144,7 @@ <h1>Preact计数器示例</h1>
144144 }
145145
146146 // 渲染应用
147- myRender ( myHtml `<${ Counter } />` , document . getElementById ( 'app' ) ) ;
147+ render ( html `< ${ Counter } /> ` , document . getElementById ( 'app' ) ) ;
148148 </ script >
149149</ body >
150150</ html >
0 commit comments