hajkmap / Hajk

A modern, full-featured OpenLayers based map viewer and editor
MIT License
122 stars 45 forks source link

Migration: Testing migration class to function component (Coordinates) #1516

Open OlofSvahnVbg opened 3 months ago

OlofSvahnVbg commented 3 months ago

The devs over at react seem to be recommending a migration from class to function components:

image

There are many pros to this and a pretty good and understandable list can be found here: https://www.geeksforgeeks.org/why-it-is-recommended-to-use-functional-components-over-class-components/

That being said, it will certainly be difficult (and exhausting) to migrate every class component to functions in HAJK. Some class components are very long and contain logic that can be hard to keep track of, lifecycle methods for instance.

An idea is to use AI to some capacity for this. I don't know how, or which AI, but it might make this easier.

OlofSvahnVbg commented 3 months ago

I tried using chatgpt and it managed to migrate this - pretty short - component:

image

OlofSvahnVbg commented 3 months ago

So react is probably going to launch a compiler some time this year (according to some sources). This compiler will render some functions and hooks obsolete (useCallback, useMemo).

The current migration from class component to function component involves memoization via useMemo, like this:

 const coordinatesModel = useMemo(
    () =>
      new CoordinatesModel({
        map: props.map,
        app: props.app,
        options: props.options,
        localObserver: localObserver,
      }),
    [props.map, props.app, props.options, localObserver]
  );

This will not be neccesary when the compiler comes out. But for now, it is.

If we remove useMemo with the current state of react, the coordinatesModel will initialize on every render, which is not ideal. image

So, the question is: do we migrate with "compiler-code" or not?

(Compiler code being without memoization)

If we migrate with "compiler-code", we might see some performance issuses due to unecceasry initializations until the compiler comes out.

If we migrate without "compiler-code", we might have to remove useMemo etc. later when the compiler comes out.

Thougts?