Skip to content

how do I use its triggerFit method in react 18? #623

Open
@linonetwo

Description

This is an old reace code written in "react": "^15.6.2", "react-dom": "^15.6.2",, how do I use its triggerFit method in react 18?

  TheGraph.App = React.createFactory(createReactClass({
    displayName: 'TheGraphApp',
    mixins,
    getDefaultProps() {
      return {
        onEdgeSelection: null,
      };
    },
    triggerFit(event) {
      const fit = geometryutils.findFit(this.props.graph, this.props.width, this.props.height, TheGraph.config.nodeSize);
      this.setState({
        x: fit.x,
        y: fit.y,
        scale: fit.scale,
      });
    },
    render() {
      // omit variable setups
      const appContents = [
        canvas,
        svg,
      ];
      const containerOptions = TheGraph.merge(TheGraph.config.app.container, { style: { width: this.state.width, height: this.state.height } });
      containerOptions.className += ` ${scaleClass}`;
      return TheGraph.factories.app.createAppContainer.call(this, containerOptions, appContents);
    },
  }));

this code is from a npm package, I can't modify its code, I just need a workaround or hack to access the method when I use this component like

  const appReference = useRef<HTMLDivElement>(null);
return <TheGraph.App
        ref={appReference}
        readonly={readonly}
        height={window.innerHeight}
        width={window.innerWidth - sidebarWidth}
        offsetX={sidebarWidth}
        getMenuDef={getMenuDef}
        onPanScale={handlePanScale}
        {...props}
      />

this component was created by React.createFactory(createReactClass({})), how can it be functional components?

But when I run the code, it did throw error Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?, why?


ChatGPT4:

The issue you're encountering is due to a fundamental shift in the way React handles components from version 15.6.2 to version 18.

In the version 15.6.2 of your code, the component was created using createReactClass, a function that creates a class-based component with lifecycle methods, and allows you to access methods (like triggerFit) via refs.

In contrast, the warning you're seeing in version 18 is because React has shifted towards function components and hooks. Function components can't be given refs to access their inner methods like class components, hence the warning. Instead, React.forwardRef is suggested, which can be used to access DOM or class components, but not functions within functional components.

Unfortunately, if you can't modify the original npm package code, it's not going to be possible to directly call the triggerFit method from outside. As the library's API doesn't provide a way to do that, it seems the library is designed for its methods to be internal.

One potential workaround would be to clone the npm package, modify it to expose the triggerFit method (for example, by adding it as a prop), and then use your modified version.

Here is an example of how you could modify the component to allow the triggerFit function to be called externally:

  TheGraph.App = React.createFactory(createReactClass({
    displayName: 'TheGraphApp',
    mixins,
    getDefaultProps() {
      return {
        onEdgeSelection: null,
        triggerFit: null, // add triggerFit in defaultProps
      };
    },
    triggerFit(event) {
      const fit = geometryutils.findFit(this.props.graph, this.props.width, this.props.height, TheGraph.config.nodeSize);
      this.setState({
        x: fit.x,
        y: fit.y,
        scale: fit.scale,
      });

      if (this.props.triggerFit) {
        this.props.triggerFit(fit);
      }
    },
    render() {
      // omit variable setups
      const appContents = [
        canvas,
        svg,
      ];
      const containerOptions = TheGraph.merge(TheGraph.config.app.container, { style: { width: this.state.width, height: this.state.height } });
      containerOptions.className += ` ${scaleClass}`;
      return TheGraph.factories.app.createAppContainer.call(this, containerOptions, appContents);
    },
  }));

And then in your component:

return <TheGraph.App
        readonly={readonly}
        height={window.innerHeight}
        width={window.innerWidth - sidebarWidth}
        offsetX={sidebarWidth}
        getMenuDef={getMenuDef}
        onPanScale={handlePanScale}
        triggerFit={(fit) => { /* you now have access to the fit result */ }}
        {...props}
      />

This will allow you to execute any code you want after triggerFit is called.

Remember to maintain your modified version of the library and keep track of any updates to the original one. In the long run, it might be beneficial to migrate to a library that is actively maintained and compatible with the latest React versions.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions