Skip to content

Commit 868ccd1

Browse files
committed
Demo: Add DomRef example
1 parent 2fa4514 commit 868ccd1

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

apps/react-hook-tracer-demo/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as contextDemo from './demos/ContextDemo'
55
import * as demo from './demos/Demo'
66
import * as counter from './demos/Counter'
77
import * as reducer from './demos/Reducer'
8+
import * as domRef from './demos/DomRef'
89
import { ItemSelector } from './ItemSelector'
910

1011
import './App.css'
@@ -14,6 +15,7 @@ const demoComponents = [
1415
{ label: 'Context', component: contextDemo.Demo },
1516
{ label: 'Counter', component: counter.Demo },
1617
{ label: 'Reducer', component: reducer.Demo },
18+
{ label: 'DOM ref', component: domRef.Demo },
1719
]
1820

1921
const sessionStorageKeyBase = '@@react-hook-tracer-demo--persistent-state:'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.parent,
2+
.child {
3+
align-self: center;
4+
display: flex;
5+
flex-direction: column;
6+
align-items: center;
7+
padding: 4px 10px 4px 10px;
8+
border-radius: 10px;
9+
}
10+
11+
.parent .trace-panel,
12+
.child .trace-panel {
13+
align-self: inherit;
14+
}
15+
16+
.parent {
17+
background-color: #ddd;
18+
}
19+
20+
.parent input {
21+
margin-top: 4px;
22+
}
23+
24+
.child {
25+
margin-top: 6px;
26+
background-color: #eee;
27+
}
28+
29+
.child input {
30+
margin-top: 4px;
31+
margin-bottom: 6px;
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-disable @typescript-eslint/no-use-before-define */
2+
import { forwardRef } from 'react'
3+
import { useRef, useTracer } from 'react-hook-tracer'
4+
5+
import './DomRef.css'
6+
7+
export const Demo = () => <Parent />
8+
9+
const Parent = () => {
10+
const { TracePanel } = useTracer()
11+
const ref = useRef<HTMLInputElement>(null)
12+
return (
13+
<div className="parent">
14+
<TracePanel />
15+
<input type="button" value="Focus on child text field" onClick={() => ref.current?.focus()} />
16+
<Child ref={ref} />
17+
</div>
18+
)
19+
}
20+
21+
const Child = forwardRef<HTMLInputElement>((_, ref) => {
22+
const { TracePanel } = useTracer()
23+
return (
24+
<div className="child">
25+
<input type="text" ref={ref} />
26+
<TracePanel />
27+
</div>
28+
)
29+
})
30+
31+
Child.displayName = 'Child'

0 commit comments

Comments
 (0)