Skip to content

Commit 798c8e1

Browse files
authored
Merge pull request #380 from plotly/refactor/functional-component
Refactor: Migrate to function components
2 parents d29e70f + ea5c15f commit 798c8e1

File tree

11 files changed

+1399
-1271
lines changed

11 files changed

+1399
-1271
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to `dash-ag-grid` will be documented in this file.
44
This project adheres to [Semantic Versioning](https://semver.org/).
55
Links "DE#nnn" prior to version 2.0 point to the Dash Enterprise closed-source Dash AG Grid repo
66

7+
## UNRELEASED
8+
9+
### Changed
10+
- Component is refactored to be a function component rather than a class
11+
712
## [32.3.0rc0] - 2025-04-15
813

914
### Fixed

package-lock.json

Lines changed: 19 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/components/AgGrid.react.js

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PropTypes from 'prop-types';
22
import LazyLoader from '../LazyLoader';
3-
import React, {Component, lazy, Suspense} from 'react';
3+
import React, {lazy, Suspense, useState, useCallback, useEffect} from 'react';
44

55
const RealAgGrid = lazy(LazyLoader.agGrid);
66
const 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+
6455
DashAgGrid.defaultProps = {
6556
className: 'ag-theme-alpine',
6657
resetColumnState: false,

0 commit comments

Comments
 (0)