question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript: Errors on react-konva refs

See original GitHub issue

When I try to simulate the hover animation example from Expand Images on Hover on my project with TypeScript with regards to React refs, I have stumbled several errors. Below is the code example that I have the errors:

import { Circle, Layer, Group, Text } from "react-konva";

interface AProps {
  ...
}

export default class A extends React.Component<AProps, {}> {
  private layer: React.RefObject<typeof Layer>;

  constructor(props: AProps) {
    super(props);
    this.layer = React.createRef<typeof Layer>();
  }

  handleExpand = e => {
    const shape = e.target;
    document.body.style.cursor = "pointer";
    shape.scaleX(1.5);
    shape.scaleY(1.5);
    this.layer.current!.draw();
  };

  handleRestore = e => {
    const shape = e.target;
    document.body.style.cursor = "default";
    shape.scaleX(1);
    shape.scaleY(1);
    this.layer.current!.draw();
  };

  render() {
    const { width, color } = this.props;
    return (
      <Layer ref={this.layer}>
        <Circle
          x={x}
          y={y}
          radius={width / 2 - 10}
          fill={color}
          stroke={'#000000'}
          strokeWidth={2}
        />
      </Layer>
    );
  }
}

The errors are as following:

  • this.layer.current!.draw(); - Property ‘draw’ does not exist on type ‘KonvaContainerComponent<Layer<Node>, LayerConfig>’.
  • ref={this.layer} - Type ‘RefObject<KonvaContainerComponent<Layer<Node>, LayerConfig>>’ is not assignable to type ‘string | ((instance: Layer<Node> | null) => void) | RefObject<Layer<Node>> | null | undefined’. Type ‘RefObject<KonvaContainerComponent<Layer<Node>, LayerConfig>>’ is not assignable to type ‘RefObject<Layer<Node>>’. Type ‘KonvaContainerComponent<Layer<Node>, LayerConfig>’ is missing the following properties from type ‘Layer<Node>’: getIntersection, enableHitGraph, disableHitGraph, clearBeforeDraw, and 128 more.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
lavrtoncommented, Aug 12, 2020

Can you make a small demo repo? I just tried this:

const App = () => {
  const ref = React.useRef<Konva.Stage>(null);
  return (
    <Stage width={window.innerWidth} height={window.innerHeight} ref={ref} />
  );
};

Works ok for me.

4reactions
zivnicommented, Feb 1, 2019

You should import the Layer for the ref from Konva not from react-konva. that is

import * as Konva from "konva"
import { Circle, Layer, Group, Text } from "react-konva";
//and then
private layer: React.RefObject<typeof Konva.Layer>;
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Konva share ref between sibling components throws error
I get 2 TypeScript errors in store/index.ts : TS2531: Object is possibly 'null'. on stageRef when I try to access stageRef.current ...
Read more >
How to access Konva nodes from react-konva?
There are two ways to access Konva nodes/shapes from react-konva . Using the refs API. You can use the refs API to get...
Read more >
react-konva + ts - CodeSandbox
TemplateReact Typescript; Environmentcreate-react-app. Files. public. src. App.tsx. index.tsx. styles.css. package.json. tsconfig.json. Dependencies. konva.
Read more >
React Konva share ref between sibling components throws error
types/index.ts. export interface IFrameItStore { downloadImage(stageRef: React.ForwardedRef<StageType>): void }. I get 2 TypeScript errors in ...
Read more >
Guide to canvas manipulation with React Konva
React Konva allows you to draw, transform, and drag shapes around the canvas much more easily than with vanilla JavaScript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found