@@ -60,7 +60,7 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
6060
6161 // set up effects
6262 useEffect ( ( ) => {
63- console . log ( 'start effect' ) ;
63+ console . log ( "useEffect: initializing" ) ;
6464 if ( ! isInitialized ) {
6565 // set up the default options for the background image
6666 const canvasSketchConfig : CanvasSketchConfig = {
@@ -87,13 +87,6 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
8787 setCanvasSketch ( newCanvasSketch ) ;
8888 setIsInitialized ( true ) ;
8989 }
90- return ( ) => {
91- // clean up
92- console . log ( 'cleanup effect' ) ;
93- if ( canvasSketch != null ) {
94- canvasSketch . dispose ( ) ;
95- }
96- }
9790 } , [
9891 canvasSketch ,
9992 htmlCanvasBackgroundImage ,
@@ -108,14 +101,42 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
108101 props . value . objects ,
109102 ] ) ;
110103
104+ useEffect ( ( ) => {
105+ console . log ( "useEffect: canvasSketch" ) ;
106+ return ( ) => {
107+ // cleanup on unmount
108+ if ( canvasSketch != null ) {
109+ canvasSketch . dispose ( ) ;
110+ }
111+ }
112+ } , [ canvasSketch ] ) ;
113+
114+ useEffect ( ( ) => {
115+ if ( canvasSketch == null ) {
116+ return ;
117+ }
118+ canvasSketch . redrawCurrentState ( ) ;
119+
120+ } , [
121+ canvasSketch ,
122+ props . canvasHeight ,
123+ props . canvasWidth ,
124+ props . containerHeight ,
125+ props . containerWidth ,
126+ ] ) ;
127+
111128 useEffect ( ( ) => {
112129 if ( canvasSketch == null ) {
113130 return ;
114131 }
132+ // Only redraw if currentObjectIndex changes but not the objects list itself. Being done to
133+ // prevent rerenders after every single stroke of the same tool. See following URL for more
134+ // inforamtion on tracking previous state/props using hooks
135+ // https://stackoverflow.com/questions/55724642/react-useeffect-hook-when-only-one-of-the-effects-deps-changes-but-not-the-oth
115136 if ( prevValueObjects === props . value . objects ) {
116137 canvasSketch . redrawSketchAt ( props . value . objects , props . value . currentObjectIndex ) ;
117138 }
118- } , [ props . value . currentObjectIndex , props . value . objects , canvasSketch , prevValueObjects ] ) ; // https://stackoverflow.com/questions/55724642/react-useeffect-hook-when-only-one-of-the-effects-deps-changes-but-not-the-oth
139+ } , [ props . value . currentObjectIndex , props . value . objects , canvasSketch , prevValueObjects ] ) ;
119140
120141 useEffect ( ( ) => {
121142 if ( canvasSketch == null ) {
@@ -146,13 +167,10 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
146167 } , [ props . toolWidth , canvasSketch ] ) ;
147168
148169 useEffect ( ( ) => {
149- console . log ( `useEffect: start` ) ;
150170 if ( canvasSketch == null ) {
151171 return ;
152172 }
153173 canvasSketch . setSelectedTool ( props . canvasToolType ) ;
154- console . log ( `useEffect: ${ props . canvasToolType } ` ) ;
155- console . log ( `useEffect: finish` ) ;
156174 } , [ props . canvasToolType , canvasSketch ] ) ;
157175
158176
0 commit comments