mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
More test cases for useCallback
Adds test cases per the previous PR for useCallback: * callback that references another callback, which in turn references a possibly-mutated value * callback that references a ref
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees:false
|
||||
import { useCallback } from "react";
|
||||
import {
|
||||
identity,
|
||||
logValue,
|
||||
makeObject_Primitives,
|
||||
useHook,
|
||||
} from "shared-runtime";
|
||||
|
||||
function Component(props) {
|
||||
const object = makeObject_Primitives();
|
||||
|
||||
useHook();
|
||||
|
||||
const log = () => {
|
||||
logValue(object);
|
||||
};
|
||||
|
||||
const onClick = useCallback(() => {
|
||||
log();
|
||||
}, [log]);
|
||||
|
||||
identity(object);
|
||||
|
||||
return <div onClick={onClick} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees:false
|
||||
import { useCallback, unstable_useMemoCache as useMemoCache } from "react";
|
||||
import {
|
||||
identity,
|
||||
logValue,
|
||||
makeObject_Primitives,
|
||||
useHook,
|
||||
} from "shared-runtime";
|
||||
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
const object = makeObject_Primitives();
|
||||
|
||||
useHook();
|
||||
|
||||
const log = () => {
|
||||
logValue(object);
|
||||
};
|
||||
|
||||
const onClick = () => {
|
||||
log();
|
||||
};
|
||||
|
||||
identity(object);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = <div onClick={onClick} />;
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) <div></div>
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// @enablePreserveExistingMemoizationGuarantees:false
|
||||
import { useCallback } from "react";
|
||||
import {
|
||||
identity,
|
||||
logValue,
|
||||
makeObject_Primitives,
|
||||
useHook,
|
||||
} from "shared-runtime";
|
||||
|
||||
function Component(props) {
|
||||
const object = makeObject_Primitives();
|
||||
|
||||
useHook();
|
||||
|
||||
const log = () => {
|
||||
logValue(object);
|
||||
};
|
||||
|
||||
const onClick = useCallback(() => {
|
||||
log();
|
||||
}, [log]);
|
||||
|
||||
identity(object);
|
||||
|
||||
return <div onClick={onClick} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback } from "react";
|
||||
import {
|
||||
identity,
|
||||
logValue,
|
||||
makeObject_Primitives,
|
||||
useHook,
|
||||
} from "shared-runtime";
|
||||
|
||||
function Component(props) {
|
||||
const object = makeObject_Primitives();
|
||||
|
||||
useHook();
|
||||
|
||||
const log = () => {
|
||||
logValue(object);
|
||||
};
|
||||
|
||||
const onClick = useCallback(() => {
|
||||
log();
|
||||
}, [log]);
|
||||
|
||||
identity(object);
|
||||
|
||||
return <div onClick={onClick} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback, unstable_useMemoCache as useMemoCache } from "react";
|
||||
import {
|
||||
identity,
|
||||
logValue,
|
||||
makeObject_Primitives,
|
||||
useHook,
|
||||
} from "shared-runtime";
|
||||
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
const object = makeObject_Primitives();
|
||||
|
||||
useHook();
|
||||
|
||||
const log = () => {
|
||||
logValue(object);
|
||||
};
|
||||
|
||||
const onClick = () => {
|
||||
log();
|
||||
};
|
||||
|
||||
identity(object);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = <div onClick={onClick} />;
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) <div></div>
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback } from "react";
|
||||
import {
|
||||
identity,
|
||||
logValue,
|
||||
makeObject_Primitives,
|
||||
useHook,
|
||||
} from "shared-runtime";
|
||||
|
||||
function Component(props) {
|
||||
const object = makeObject_Primitives();
|
||||
|
||||
useHook();
|
||||
|
||||
const log = () => {
|
||||
logValue(object);
|
||||
};
|
||||
|
||||
const onClick = useCallback(() => {
|
||||
log();
|
||||
}, [log]);
|
||||
|
||||
identity(object);
|
||||
|
||||
return <div onClick={onClick} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback, useRef } from "react";
|
||||
|
||||
function Component(props) {
|
||||
const ref = useRef(null);
|
||||
|
||||
const onChange = useCallback((event) => {
|
||||
// The ref should still be mutable here even though function deps are frozen in
|
||||
// @enablePreserveExistingMemoizationGuarantees mode
|
||||
ref.current = event.target.value;
|
||||
});
|
||||
|
||||
return <input onChange={onChange} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import {
|
||||
useCallback,
|
||||
useRef,
|
||||
unstable_useMemoCache as useMemoCache,
|
||||
} from "react";
|
||||
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(3);
|
||||
const ref = useRef(null);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = (event) => {
|
||||
ref.current = event.target.value;
|
||||
};
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
const onChange = t0;
|
||||
let t1;
|
||||
if ($[1] !== onChange) {
|
||||
t1 = <input onChange={onChange} />;
|
||||
$[1] = onChange;
|
||||
$[2] = t1;
|
||||
} else {
|
||||
t1 = $[2];
|
||||
}
|
||||
return t1;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) <input>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback, useRef } from "react";
|
||||
|
||||
function Component(props) {
|
||||
const ref = useRef(null);
|
||||
|
||||
const onChange = useCallback((event) => {
|
||||
// The ref should still be mutable here even though function deps are frozen in
|
||||
// @enablePreserveExistingMemoizationGuarantees mode
|
||||
ref.current = event.target.value;
|
||||
});
|
||||
|
||||
return <input onChange={onChange} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback, useRef } from "react";
|
||||
|
||||
function Component(props) {
|
||||
const ref = useRef(null);
|
||||
|
||||
const onChange = useCallback((event) => {
|
||||
// The ref should still be mutable here even though function deps are frozen in
|
||||
// @enablePreserveExistingMemoizationGuarantees mode
|
||||
ref.current = event.target.value;
|
||||
});
|
||||
|
||||
return <input onChange={onChange} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import {
|
||||
useCallback,
|
||||
useRef,
|
||||
unstable_useMemoCache as useMemoCache,
|
||||
} from "react";
|
||||
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(3);
|
||||
const ref = useRef(null);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = (event) => {
|
||||
ref.current = event.target.value;
|
||||
};
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
const onChange = t0;
|
||||
let t1;
|
||||
if ($[1] !== onChange) {
|
||||
t1 = <input onChange={onChange} />;
|
||||
$[1] = onChange;
|
||||
$[2] = t1;
|
||||
} else {
|
||||
t1 = $[2];
|
||||
}
|
||||
return t1;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) <input>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// @enablePreserveExistingMemoizationGuarantees
|
||||
import { useCallback, useRef } from "react";
|
||||
|
||||
function Component(props) {
|
||||
const ref = useRef(null);
|
||||
|
||||
const onChange = useCallback((event) => {
|
||||
// The ref should still be mutable here even though function deps are frozen in
|
||||
// @enablePreserveExistingMemoizationGuarantees mode
|
||||
ref.current = event.target.value;
|
||||
});
|
||||
|
||||
return <input onChange={onChange} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
@@ -153,6 +153,10 @@ export function throwInput(x: Object): never {
|
||||
throw x;
|
||||
}
|
||||
|
||||
export function logValue<T>(value: T): void {
|
||||
console.log(value);
|
||||
}
|
||||
|
||||
export function useHook(): Object {
|
||||
return makeObject_Primitives();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user