File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { h , render } from "preact" ;
2+ import { useState } from "preact/hooks" ;
3+ import { memo } from "preact/compat" ;
4+
5+ export const Result = ( { result } ) => {
6+ return (
7+ < div class = "result" >
8+ < Nested text = { result } />
9+ </ div >
10+ ) ;
11+ } ;
12+
13+ const Nested = ( { text } ) => {
14+ return < p > { text } </ p > ;
15+ } ;
16+
17+ const MemoResult = memo ( Result , ( ) => true ) ;
18+
19+ const generateFakeData = ( ) => {
20+ const rand = ( ) => Math . random ( ) * 10 ;
21+ return new Array ( 3 ) . fill ( rand ( ) ) ;
22+ } ;
23+
24+ const App = ( ) => {
25+ const [ results , setResults ] = useState ( generateFakeData ( ) ) ;
26+
27+ return (
28+ < div >
29+ < h1 > Example</ h1 >
30+ < button onClick = { ( ) => setResults ( generateFakeData ( ) ) } > Refresh</ button >
31+ < div class = "list" >
32+ < h2 > No memo</ h2 >
33+ { results . map ( ( result , i ) => (
34+ < Result key = { i } result = { result } />
35+ ) ) }
36+ </ div >
37+ < div class = "list" >
38+ < h2 > Memo</ h2 >
39+ { results . map ( ( result , i ) => (
40+ < MemoResult key = { i } result = { result } />
41+ ) ) }
42+ </ div >
43+ </ div >
44+ ) ;
45+ } ;
46+
47+ render ( < App /> , document . getElementById ( "app" ) ) ;
You can’t perform that action at this time.
0 commit comments