From dea6850e85993e4e66c4878a048d438f558f8b60 Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Fri, 19 Mar 2021 10:41:08 +0100 Subject: [PATCH] Add details to useEffect and class components --- JavaScript/React/React.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/JavaScript/React/React.md b/JavaScript/React/React.md index eeae63c..459f68f 100644 --- a/JavaScript/React/React.md +++ b/JavaScript/React/React.md @@ -114,6 +114,8 @@ class Button extends React.Component { super(props); this.state = {count: 0}; } + + componentDidMount() {} // called on successful component mount handleClick = () => { this.setState({ count: this.state.count + 1 }); @@ -223,14 +225,15 @@ const [state, setState] = useState(default); ### `useEffect` -Hook used to trigger an action on each reder of the component. +Hook used to trigger an action on each reder of the component or when one of the watched items changes. ```js -useEffect(() => - // introduce side effect of the render - return () => {/* clean up side effect */} -); +useEffect(() => { + // "side effects" operations + + return () => {/* clean up side effect */} // optional +}, [/* list of watched items, empty triggers once */]); ``` ### Custom Hooks