Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React class to functional component #706

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/components/containers/EventsBrowser.tsx
pi1814 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ interface EventBrowserState {
exportTooltip: boolean;
}

@Resizer(BreakpointConfig)
class EventsBrowser extends React.Component<EventBrowserProps, EventBrowserState> {
constructor(props) {
super(props);
Expand Down Expand Up @@ -631,4 +630,4 @@ export default connect(
return dispatch(clearSession());
},
})
)(EventsBrowser);
)(Resizer(BreakpointConfig)(EventsBrowser));
53 changes: 26 additions & 27 deletions src/components/containers/wrappers/Resize.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import React, { useEffect, useState } from "react";
import _ from "lodash";
const handlers: any[] = [];

const handlers: Array<() => void> = [];

function defferedHandlerCaller() {
handlers.forEach((handle) => {
Expand All @@ -19,16 +20,11 @@ export function Resizer(config: Config) {
const { onResize, debounce } = config || {};
const debounceTime = debounce || 500;

return function decorateClass(DecoratedComponent) {
return class Resize extends React.Component {
private _registeredIndex: number;
constructor(props) {
super(props);
this.state = this.getState();
this.onWindowResize = _.debounce(this.onWindowResize.bind(this), debounceTime);
}
return function decorateClass(DecoratedComponent: React.ComponentType<any>) {
const Resize: React.FC<any> = (props) => {
const [state, setState] = useState(() => getInitialState());

getState() {
function getInitialState() {
if (typeof onResize === "function") {
const determinedWindow = typeof window === "object" ? window : {};
const newState = onResize(determinedWindow);
Expand All @@ -39,27 +35,30 @@ export function Resizer(config: Config) {
return {};
}

onWindowResize() {
this.setState(this.getState());
}
const onWindowResize = _.debounce(() => {
setState(getInitialState());
}, debounceTime);

componentDidMount() {
this._registeredIndex = handlers.length;
handlers.push(this.onWindowResize);
useEffect(() => {
const registeredIndex = handlers.length;
handlers.push(onWindowResize);

window.addEventListener("resize", () => {
const resizeListener = () => {
setTimeout(defferedHandlerCaller, 0);
});
}
};

componentWillUnmount() {
// just place null in place to not throw off index
handlers.splice(this._registeredIndex, 1, null);
}
window.addEventListener("resize", resizeListener);

render() {
return <DecoratedComponent {...this.props} {...this.state} />;
}
return () => {
// Clean up the handler
handlers.splice(registeredIndex, 1, null);
window.removeEventListener("resize", resizeListener);
};
}, [onWindowResize]);

return <DecoratedComponent {...props} {...state} />;
};

return Resize;
};
}
Loading