File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed
examples/browsers/libraries/reactjs Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -5,15 +5,19 @@ const { createElement: ce, useState } = React;
5
5
6
6
ReactDOMClient . createRoot ( document . getElementById ( 'root' ) ) . render ( ce ( App ) ) ;
7
7
8
- function createFactory ( fn ) {
8
+ /**
9
+ * @param {Function } fn - The function to be memoized.
10
+ * @returns {Function } - A memoized version of the function.
11
+ */
12
+ function functionFactory ( fn ) {
9
13
let fnCache = null ;
10
14
let depsCache = [ ] ;
11
15
12
16
return ( ...args ) => {
13
17
if (
14
18
fnCache === null ||
15
19
depsCache . length !== args . length ||
16
- args . some ( ( dep , i ) => dep !== depsCache [ i ] )
20
+ ! args . every ( ( dep , i ) => Object . is ( dep , depsCache [ i ] ) )
17
21
) {
18
22
fnCache = fn . apply ( undefined , args ) ;
19
23
depsCache = args ;
@@ -23,14 +27,14 @@ function createFactory(fn) {
23
27
} ;
24
28
}
25
29
26
- const OnClickFactory = createFactory ( setCount => _event => setCount ( prev => prev + 1 ) ) ;
30
+ const onClickFactory = functionFactory ( setCount => _event => setCount ( prev => prev + 1 ) ) ;
27
31
28
32
const stack = [ ] ;
29
33
30
34
function App ( ) {
31
35
const [ count , setCount ] = useState ( 0 ) ;
32
36
33
- const onClick = OnClickFactory ( setCount ) ;
37
+ const onClick = onClickFactory ( setCount ) ;
34
38
35
39
stack . push ( onClick ) ;
36
40
You can’t perform that action at this time.
0 commit comments