4
4
*/
5
5
6
6
import { CollapseTransition , Divider , Button } from '@tool-pack/react-ui' ;
7
- import React , { useLayoutEffect , useState } from 'react' ;
7
+ import React , { useLayoutEffect , useState , useRef } from 'react' ;
8
+
8
9
const App : React . FC = ( ) => {
9
10
const [ visible , setVisible ] = useState ( true ) ;
11
+ const elRef = useRef < HTMLDivElement > ( null ) ;
12
+
10
13
useLayoutEffect ( ( ) => {
11
- // 该元素的ref已经被Transition组件获取了,无法再在外部靠useRef获取
12
- const el = document . querySelector < HTMLDivElement > (
13
- '#collapse-horizontal2-demo' ,
14
- ) ;
14
+ const el = elRef . current ;
15
15
if ( ! el ) return ;
16
16
17
17
const timer = setTimeout ( ( ) => {
@@ -21,14 +21,16 @@ const App: React.FC = () => {
21
21
22
22
return ( ) => clearTimeout ( timer ) ;
23
23
} , [ ] ) ;
24
+
24
25
return (
25
26
< div >
26
27
< div style = { { paddingBottom : '8px' , textAlign : 'center' } } >
27
28
< Button onClick = { ( ) => setVisible ( ( v ) => ! v ) } > toggle</ Button >
28
29
</ div >
29
30
< CollapseTransition show = { visible } width >
30
31
< div style = { { background : '#910852' , color : 'white' } } >
31
- < div id = "collapse-horizontal2-demo" style = { { padding : '10px' } } >
32
+ { /* 下面这层 div 是为了固定宽度 */ }
33
+ < div style = { { padding : '10px' } } ref = { elRef } >
32
34
< p >
33
35
载(zài)营魄抱一,能无离乎?专气致柔,能婴儿乎?涤除玄览,能无疵乎?爱民治国,能无知(zhì)乎?天门开阖(hé),能无雌乎?明白四达,能无为乎?生之、畜(xù)之,生而不有,为而不恃,长(zhǎng)而不宰,是谓玄德。
34
36
</ p >
0 commit comments