@@ -6,6 +6,7 @@ import React, { useEffect, useMemo, useState } from "react";
66
77const TableRender = ( { columns, url, include = [ ] , exclude = [ ] } ) : React . JSX . Element => {
88 const [ jsonContent , setJsonContent ] = useState ( [ ] ) ;
9+ const [ isLoading , setIsLoading ] = useState ( true ) ;
910 const { colorMode } = useColorMode ( ) ;
1011
1112 const theme = useMemo ( ( ) => {
@@ -21,9 +22,19 @@ const TableRender = ({ columns, url, include = [], exclude = [] }): React.JSX.El
2122 } ) ;
2223 } , [ colorMode ] ) ;
2324
25+ // Memoize the include and exclude arrays to prevent unnecessary re-renders
26+ const memoizedInclude = useMemo ( ( ) => include , [ JSON . stringify ( include ) ] ) ;
27+ const memoizedExclude = useMemo ( ( ) => exclude , [ JSON . stringify ( exclude ) ] ) ;
28+
2429 useEffect ( ( ) => {
30+ setIsLoading ( true ) ;
2531 fetch ( url )
26- . then ( ( res ) => res . json ( ) )
32+ . then ( ( res ) => {
33+ if ( ! res . ok ) {
34+ throw new Error ( `HTTP error! status: ${ res . status } ` ) ;
35+ }
36+ return res . json ( ) ;
37+ } )
2738 . then ( ( data ) => {
2839 const updatedData = [ ] ;
2940 const names = [ ] ;
@@ -32,12 +43,12 @@ const TableRender = ({ columns, url, include = [], exclude = [] }): React.JSX.El
3243 // filter duplicate names
3344 const item = data [ key ] ;
3445 const name = item . name ;
35- for ( const element of exclude ) {
46+ for ( const element of memoizedExclude ) {
3647 if ( name . includes ( element ) ) {
3748 return ;
3849 }
3950 }
40- for ( const element of include ) {
51+ for ( const element of memoizedInclude ) {
4152 if ( ! name . includes ( element ) ) {
4253 return ;
4354 }
@@ -58,11 +69,16 @@ const TableRender = ({ columns, url, include = [], exclude = [] }): React.JSX.El
5869 } ) ;
5970
6071 setJsonContent ( updatedData ) ;
72+ setIsLoading ( false ) ;
73+ } )
74+ . catch ( ( error ) => {
75+ console . error ( "Error fetching data:" , error ) ;
76+ setIsLoading ( false ) ;
6177 } ) ;
62- // execute this fetch only once (on mount)
63- } , [ include , exclude , url ] ) ;
78+ // Only re- fetch when url, include, or exclude actually change
79+ } , [ url , memoizedInclude , memoizedExclude ] ) ;
6480
65- if ( ! columns || ! jsonContent ) {
81+ if ( ! columns || ( ! jsonContent && ! isLoading ) ) {
6682 return null ;
6783 }
6884
@@ -81,6 +97,7 @@ const TableRender = ({ columns, url, include = [], exclude = [] }): React.JSX.El
8197 muiPaginationProps = { {
8298 rowsPerPageOptions : [ 10 , 15 , 25 , 50 , 100 ] ,
8399 } }
100+ state = { { isLoading } }
84101 />
85102 </ ThemeProvider >
86103 ) }
0 commit comments