@@ -1343,33 +1343,6 @@ if (__EXPERIMENTAL__) {
1343
1343
}
1344
1344
` ,
1345
1345
} ,
1346
- {
1347
- code : normalizeIndent `
1348
- // Valid because functions created with useEffectEvent can be called in closures.
1349
- function MyComponent({ theme }) {
1350
- const onClick = useEffectEvent(() => {
1351
- showNotification(theme);
1352
- });
1353
- return <Child onClick={() => onClick()}></Child>;
1354
- }
1355
- ` ,
1356
- } ,
1357
- {
1358
- code : normalizeIndent `
1359
- // Valid because functions created with useEffectEvent can be called in closures.
1360
- function MyComponent({ theme }) {
1361
- const onClick = useEffectEvent(() => {
1362
- showNotification(theme);
1363
- });
1364
- const onClick2 = () => { onClick() };
1365
- const onClick3 = useCallback(() => onClick(), []);
1366
- return <>
1367
- <Child onClick={onClick2}></Child>
1368
- <Child onClick={onClick3}></Child>
1369
- </>;
1370
- }
1371
- ` ,
1372
- } ,
1373
1346
{
1374
1347
code : normalizeIndent `
1375
1348
// Valid because functions created with useEffectEvent can be passed by reference in useEffect
@@ -1380,47 +1353,39 @@ if (__EXPERIMENTAL__) {
1380
1353
});
1381
1354
const onClick2 = useEffectEvent(() => {
1382
1355
debounce(onClick);
1356
+ debounce(() => onClick());
1357
+ debounce(() => { onClick() });
1358
+ deboucne(() => debounce(onClick));
1383
1359
});
1384
1360
useEffect(() => {
1385
- let id = setInterval(onClick, 100);
1361
+ let id = setInterval(() => onClick() , 100);
1386
1362
return () => clearInterval(onClick);
1387
1363
}, []);
1388
- return <Child onClick={() => onClick2()} />
1364
+ return null;
1389
1365
}
1390
1366
` ,
1391
1367
} ,
1392
- {
1393
- code : normalizeIndent `
1394
- const MyComponent = ({theme}) => {
1395
- const onClick = useEffectEvent(() => {
1396
- showNotification(theme);
1397
- });
1398
- return <Child onClick={() => onClick()}></Child>;
1399
- };
1400
- ` ,
1401
- } ,
1402
1368
{
1403
1369
code : normalizeIndent `
1404
1370
function MyComponent({ theme }) {
1405
- const notificationService = useNotifications();
1406
- const showNotification = useEffectEvent((text) => {
1407
- notificationService.notify(theme, text);
1371
+ useEffect(() => {
1372
+ onClick();
1408
1373
});
1409
- const onClick = useEffectEvent((text ) => {
1410
- showNotification(text );
1374
+ const onClick = useEffectEvent(() => {
1375
+ showNotification(theme );
1411
1376
});
1412
- return <Child onClick={(text) => onClick(text)} />
1413
1377
}
1414
1378
` ,
1415
1379
} ,
1416
1380
{
1417
1381
code : normalizeIndent `
1418
1382
function MyComponent({ theme }) {
1419
- useEffect(( ) => {
1420
- onClick( );
1383
+ const onEvent = useEffectEvent((text ) => {
1384
+ console.log(text );
1421
1385
});
1422
- const onClick = useEffectEvent(() => {
1423
- showNotification(theme);
1386
+
1387
+ useEffect(() => {
1388
+ onEvent('Hello world');
1424
1389
});
1425
1390
}
1426
1391
` ,
@@ -1437,7 +1402,7 @@ if (__EXPERIMENTAL__) {
1437
1402
return <Child onClick={onClick}></Child>;
1438
1403
}
1439
1404
` ,
1440
- errors : [ useEffectEventError ( 'onClick' ) ] ,
1405
+ errors : [ useEffectEventError ( 'onClick' , false ) ] ,
1441
1406
} ,
1442
1407
{
1443
1408
code : normalizeIndent `
@@ -1456,8 +1421,23 @@ if (__EXPERIMENTAL__) {
1456
1421
});
1457
1422
return <Child onClick={() => onClick()} />
1458
1423
}
1424
+
1425
+ // The useEffectEvent function shares an identifier name with the above
1426
+ function MyLastComponent({theme}) {
1427
+ const onClick = useEffectEvent(() => {
1428
+ showNotification(theme)
1429
+ });
1430
+ useEffect(() => {
1431
+ onClick(); // No error here, errors on all other uses
1432
+ onClick;
1433
+ })
1434
+ return <Child />
1435
+ }
1459
1436
` ,
1460
- errors : [ { ...useEffectEventError ( 'onClick' ) , line : 7 } ] ,
1437
+ errors : [
1438
+ { ...useEffectEventError ( 'onClick' , false ) , line : 7 } ,
1439
+ { ...useEffectEventError ( 'onClick' , true ) , line : 15 } ,
1440
+ ] ,
1461
1441
} ,
1462
1442
{
1463
1443
code : normalizeIndent `
@@ -1468,7 +1448,7 @@ if (__EXPERIMENTAL__) {
1468
1448
return <Child onClick={onClick}></Child>;
1469
1449
}
1470
1450
` ,
1471
- errors : [ useEffectEventError ( 'onClick' ) ] ,
1451
+ errors : [ useEffectEventError ( 'onClick' , false ) ] ,
1472
1452
} ,
1473
1453
{
1474
1454
code : normalizeIndent `
@@ -1481,7 +1461,7 @@ if (__EXPERIMENTAL__) {
1481
1461
return <Bar onClick={foo} />
1482
1462
}
1483
1463
` ,
1484
- errors : [ { ...useEffectEventError ( 'onClick' ) , line : 7 } ] ,
1464
+ errors : [ { ...useEffectEventError ( 'onClick' , false ) , line : 7 } ] ,
1485
1465
} ,
1486
1466
{
1487
1467
code : normalizeIndent `
@@ -1497,7 +1477,27 @@ if (__EXPERIMENTAL__) {
1497
1477
return <Child onClick={onClick} />
1498
1478
}
1499
1479
` ,
1500
- errors : [ useEffectEventError ( 'onClick' ) ] ,
1480
+ errors : [ useEffectEventError ( 'onClick' , false ) ] ,
1481
+ } ,
1482
+ {
1483
+ code : normalizeIndent `
1484
+ // Invalid because functions created with useEffectEvent cannot be called in arbitrary closures.
1485
+ function MyComponent({ theme }) {
1486
+ const onClick = useEffectEvent(() => {
1487
+ showNotification(theme);
1488
+ });
1489
+ const onClick2 = () => { onClick() };
1490
+ const onClick3 = useCallback(() => onClick(), []);
1491
+ return <>
1492
+ <Child onClick={onClick2}></Child>
1493
+ <Child onClick={onClick3}></Child>
1494
+ </>;
1495
+ }
1496
+ ` ,
1497
+ errors : [
1498
+ useEffectEventError ( 'onClick' , true ) ,
1499
+ useEffectEventError ( 'onClick' , true ) ,
1500
+ ] ,
1501
1501
} ,
1502
1502
] ;
1503
1503
}
@@ -1559,11 +1559,11 @@ function classError(hook) {
1559
1559
} ;
1560
1560
}
1561
1561
1562
- function useEffectEventError ( fn ) {
1562
+ function useEffectEventError ( fn , called ) {
1563
1563
return {
1564
1564
message :
1565
1565
`\`${ fn } \` is a function created with React Hook "useEffectEvent", and can only be called from ` +
1566
- ' the same component. They cannot be assigned to variables or passed down.',
1566
+ ` the same component.${ called ? '' : ' They cannot be assigned to variables or passed down.'} ` ,
1567
1567
} ;
1568
1568
}
1569
1569
0 commit comments