11import PropTypes from 'prop-types' ;
22import LazyLoader from '../LazyLoader' ;
3- import React , { Component , lazy , Suspense } from 'react' ;
3+ import React , { lazy , Suspense , useState , useCallback , useEffect } from 'react' ;
44
55const RealAgGrid = lazy ( LazyLoader . agGrid ) ;
66const RealAgGridEnterprise = lazy ( LazyLoader . agGridEnterprise ) ;
@@ -12,55 +12,46 @@ function getGrid(enable) {
1212/**
1313 * Dash interface to AG Grid, a powerful tabular data component.
1414 */
15- class DashAgGrid extends Component {
16- constructor ( props ) {
17- super ( props ) ;
15+ function DashAgGrid ( props ) {
16+ const [ state , setState ] = useState ( {
17+ mounted : false ,
18+ rowTransaction : null ,
19+ } ) ;
1820
19- this . state = {
20- mounted : false ,
21- rowTransaction : null ,
22- } ;
23-
24- this . buildArray = this . buildArray . bind ( this ) ;
25- }
26-
27- static dashRenderType = true ;
28-
29- buildArray ( arr1 , arr2 ) {
21+ const buildArray = useCallback ( ( arr1 , arr2 ) => {
3022 if ( arr1 ) {
3123 if ( ! arr1 . includes ( arr2 ) ) {
3224 return [ ...arr1 , arr2 ] ;
3325 }
3426 return arr1 ;
3527 }
3628 return [ JSON . parse ( JSON . stringify ( arr2 ) ) ] ;
37- }
38-
39- UNSAFE_componentWillReceiveProps ( nextProps ) {
40- if ( this . props . rowTransaction && ! this . state . mounted ) {
41- if ( nextProps . rowTransaction !== this . props . rowTransaction ) {
42- this . setState ( {
43- rowTransaction : this . buildArray (
44- this . state . rowTransaction ,
45- this . props . rowTransaction
46- ) ,
47- } ) ;
48- }
29+ } , [ ] ) ;
30+
31+ useEffect ( ( ) => {
32+ if ( props . rowTransaction && ! state . mounted ) {
33+ setState ( ( prevState ) => ( {
34+ ...prevState ,
35+ rowTransaction : buildArray (
36+ prevState . rowTransaction ,
37+ props . rowTransaction
38+ ) ,
39+ } ) ) ;
4940 }
50- }
41+ } , [ props . rowTransaction , state . mounted , buildArray ] ) ;
5142
52- render ( ) {
53- const { enableEnterpriseModules } = this . props ;
43+ const { enableEnterpriseModules } = props ;
44+ const RealComponent = getGrid ( enableEnterpriseModules ) ;
5445
55- const RealComponent = getGrid ( enableEnterpriseModules ) ;
56- return (
57- < Suspense fallback = { null } >
58- < RealComponent parentState = { this . state } { ...this . props } />
59- </ Suspense >
60- ) ;
61- }
46+ return (
47+ < Suspense fallback = { null } >
48+ < RealComponent parentState = { state } { ...props } />
49+ </ Suspense >
50+ ) ;
6251}
6352
53+ DashAgGrid . dashRenderType = true ;
54+
6455DashAgGrid . defaultProps = {
6556 className : 'ag-theme-alpine' ,
6657 resetColumnState : false ,
0 commit comments