Skip to content

Commit e4a2e0d

Browse files
EFFECT
1 parent b92e15a commit e4a2e0d

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

react.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,4 +1076,38 @@ const FunctionalComponent = () => {
10761076
{show && <p>{show ? `Ì'm visible` : `Ì'm not visible`}</p>}
10771077
</div>
10781078
);
1079-
};
1079+
};
1080+
1081+
1082+
1083+
// EFFECT
1084+
import { useEffect } from 'react';
1085+
const FunctionalComponent = () => {
1086+
// On Mounting
1087+
useEffect( () => console.log("mount"), [] );
1088+
// update specific data
1089+
useEffect( () => console.log("will update data"), [ data ] );
1090+
// update all
1091+
useEffect( () => console.log("will update any") );
1092+
// update specific data or unmount
1093+
useEffect( () => () => console.log("will update data or unmount"), [ data ] );
1094+
// On Unmounting
1095+
useEffect( () => () => console.log("unmount"), [] );
1096+
// updated data returned
1097+
return <h1>{data}</h1>;
1098+
};
1099+
1100+
// skip first render with useEffect
1101+
import { useEffect, useRef } from 'react';
1102+
const FunctionalComponent = () => {
1103+
// useref to avoid wasted renders
1104+
const notInitialRender = useRef(false)
1105+
useEffect(() => {
1106+
if (notInitialRender.current) {
1107+
// do your magic here
1108+
} else {
1109+
notInitialRender.current = true
1110+
}
1111+
}, [data])
1112+
return <h1>{data}</h1>;
1113+
}

0 commit comments

Comments
 (0)