@@ -1343,33 +1343,6 @@ if (__EXPERIMENTAL__) {
13431343 }
13441344 ` ,
13451345 } ,
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- } ,
13731346 {
13741347 code : normalizeIndent `
13751348 // Valid because functions created with useEffectEvent can be passed by reference in useEffect
@@ -1380,47 +1353,39 @@ if (__EXPERIMENTAL__) {
13801353 });
13811354 const onClick2 = useEffectEvent(() => {
13821355 debounce(onClick);
1356+ debounce(() => onClick());
1357+ debounce(() => { onClick() });
1358+ deboucne(() => debounce(onClick));
13831359 });
13841360 useEffect(() => {
1385- let id = setInterval(onClick, 100);
1361+ let id = setInterval(() => onClick() , 100);
13861362 return () => clearInterval(onClick);
13871363 }, []);
1388- return <Child onClick={() => onClick2()} />
1364+ return null;
13891365 }
13901366 ` ,
13911367 } ,
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- } ,
14021368 {
14031369 code : normalizeIndent `
14041370 function MyComponent({ theme }) {
1405- const notificationService = useNotifications();
1406- const showNotification = useEffectEvent((text) => {
1407- notificationService.notify(theme, text);
1371+ useEffect(() => {
1372+ onClick();
14081373 });
1409- const onClick = useEffectEvent((text ) => {
1410- showNotification(text );
1374+ const onClick = useEffectEvent(() => {
1375+ showNotification(theme );
14111376 });
1412- return <Child onClick={(text) => onClick(text)} />
14131377 }
14141378 ` ,
14151379 } ,
14161380 {
14171381 code : normalizeIndent `
14181382 function MyComponent({ theme }) {
1419- useEffect(( ) => {
1420- onClick( );
1383+ const onEvent = useEffectEvent((text ) => {
1384+ console.log(text );
14211385 });
1422- const onClick = useEffectEvent(() => {
1423- showNotification(theme);
1386+
1387+ useEffect(() => {
1388+ onEvent('Hello world');
14241389 });
14251390 }
14261391 ` ,
@@ -1437,7 +1402,7 @@ if (__EXPERIMENTAL__) {
14371402 return <Child onClick={onClick}></Child>;
14381403 }
14391404 ` ,
1440- errors : [ useEffectEventError ( 'onClick' ) ] ,
1405+ errors : [ useEffectEventError ( 'onClick' , false ) ] ,
14411406 } ,
14421407 {
14431408 code : normalizeIndent `
@@ -1456,8 +1421,23 @@ if (__EXPERIMENTAL__) {
14561421 });
14571422 return <Child onClick={() => onClick()} />
14581423 }
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+ }
14591436 ` ,
1460- errors : [ { ...useEffectEventError ( 'onClick' ) , line : 7 } ] ,
1437+ errors : [
1438+ { ...useEffectEventError ( 'onClick' , false ) , line : 7 } ,
1439+ { ...useEffectEventError ( 'onClick' , true ) , line : 15 } ,
1440+ ] ,
14611441 } ,
14621442 {
14631443 code : normalizeIndent `
@@ -1468,7 +1448,7 @@ if (__EXPERIMENTAL__) {
14681448 return <Child onClick={onClick}></Child>;
14691449 }
14701450 ` ,
1471- errors : [ useEffectEventError ( 'onClick' ) ] ,
1451+ errors : [ useEffectEventError ( 'onClick' , false ) ] ,
14721452 } ,
14731453 {
14741454 code : normalizeIndent `
@@ -1481,7 +1461,7 @@ if (__EXPERIMENTAL__) {
14811461 return <Bar onClick={foo} />
14821462 }
14831463 ` ,
1484- errors : [ { ...useEffectEventError ( 'onClick' ) , line : 7 } ] ,
1464+ errors : [ { ...useEffectEventError ( 'onClick' , false ) , line : 7 } ] ,
14851465 } ,
14861466 {
14871467 code : normalizeIndent `
@@ -1497,7 +1477,27 @@ if (__EXPERIMENTAL__) {
14971477 return <Child onClick={onClick} />
14981478 }
14991479 ` ,
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+ ] ,
15011501 } ,
15021502 ] ;
15031503}
@@ -1559,11 +1559,11 @@ function classError(hook) {
15591559 } ;
15601560}
15611561
1562- function useEffectEventError ( fn ) {
1562+ function useEffectEventError ( fn , called ) {
15631563 return {
15641564 message :
15651565 `\`${ 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.'} ` ,
15671567 } ;
15681568}
15691569
0 commit comments