React Konva
React Konva provides declarative and reactive bindings for integrating the Konva 2D canvas framework into React applications. It allows developers to use Konva's rich set of shapes, layers, and stage components as standard React components, enabling a familiar declarative JSX syntax for canvas drawing. The library is currently at version 19.2.3, frequently releasing patch and minor updates to ensure compatibility with the latest React and Konva versions. Its primary differentiator is leveraging React's component model and data flow for high-performance canvas rendering, abstracting away direct DOM manipulation. It is built on top of the Konva framework, meaning users primarily need to understand Konva's API and concepts to effectively use React Konva.
Common errors
-
Objects are not valid as a React child (found: object with keys {x, y, ...}). If you meant to render a collection of children, use an array instead.cause Attempting to render a raw Konva object or an incorrect JSX structure directly within a React component.fixEnsure all children within `<Stage>` and `<Layer>` are valid React-Konva components (e.g., `<Rect>`, `<Circle>`) and not raw Konva instances or other non-renderable objects. -
"Konva component" is not a <Stage> child. <Stage> can only contain <Layer> children.
cause Placing Konva shapes directly inside `<Stage>` instead of wrapping them in a `<Layer>`. The `<Stage>` component is a container for `<Layer>` components.fixAll visual Konva components (e.g., `<Rect>`, `<Circle>`, `<Text>`) must be nested inside a `<Layer>` component, which in turn is a child of the `<Stage>` component. -
Property 'current' does not exist on type 'MutableRefObject<undefined>'.
cause Accessing the `.current` property of a `useRef` hook before the component has mounted or before the ref has been assigned to the Konva node instance.fixEnsure you access `ref.current` within a `useEffect` hook with an empty dependency array (`[]`) to guarantee the component is mounted and the ref has been assigned to the Konva node instance. -
ReferenceError: window is not defined
cause Attempting to use `window.innerWidth` or `window.innerHeight` (or other browser-specific globals) in a server-side rendering (SSR) context, where `window` is not available.fixFor SSR, conditionally render the `<Stage>` component only on the client-side, provide default dimensions, or use a client-only rendering approach for the canvas component if browser globals are required for initial sizing.
Warnings
- gotcha React Konva is not supported in React Native environments. It is specifically designed for web-based applications using the HTML5 canvas.
- gotcha Understanding Konva's "Strict Mode" is crucial for managing component property updates. By default, react-konva only updates properties changed in render(). In strict mode, all properties are updated to match render() values, potentially overwriting manual changes.
- breaking Compatibility with React 19. React Konva v19 requires React 19.2.0 or higher. Earlier versions of react-konva are not compatible with React 19.
- breaking Compatibility with Konva v10. React Konva v19.0.8 added support for Konva v10. Earlier v19 versions might have limited or no compatibility.
- gotcha Double event attachments in React Strict Mode were a known issue in earlier v19 versions, leading to unexpected event behavior.
Install
-
npm install react-konva -
yarn add react-konva -
pnpm add react-konva
Imports
- Stage, Layer, Rect, Text
import Stage from 'react-konva/Stage';
import { Stage, Layer, Rect, Text } from 'react-konva'; - useStrictMode
import useStrictMode from 'react-konva';
import { useStrictMode } from 'react-konva'; - Konva
import { Konva } from 'konva';import Konva from 'konva';
- Circle
import Circle from 'react-konva';
import { Circle } from 'react-konva';
Quickstart
import React, { useState } from 'react';
import { render } from 'react-dom';
import { Stage, Layer, Rect, Text } from 'react-konva';
import Konva from 'konva';
const ColoredRect = () => {
const [color, setColor] = useState('green');
const handleClick = () => {
setColor(Konva.Util.getRandomColor());
};
return <Rect x={20} y={20} width={50} height={50} fill={color} shadowBlur={5} onClick={handleClick} />;
};
const App = () => {
return (
<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer>
<Text text="Try click on rect" />
<ColoredRect />
</Layer>
</Stage>
);
};
render(<App />, document.getElementById('root'));