Skip to content

How to store data in state in canvas? #124

Answered by 9uifranco
shibilainfoskies asked this question in Q&A
Discussion options

You must be logged in to vote

I know this is old but I thought it might still be helpful for someone going through a similar situation.

To store the canvas paths you can access exportPaths method using ref like this:

const canvasRef = useRef<ReactSketchCanvasRef>(null);

<ReactSketchCanvas
    ref={canvasRef}
    onChange={handleCanvasOnChange}
    style={styles}
    width="600"
    height="800"
    strokeWidth={4}
    strokeColor="black"
/>

Then, you can create a Save button or use the onChange prop to save it every time the user interacts with the canvas (if you choose the second method I think you could add some debounce logic to avoid saving too frequently)

const [canvas, setCanvas] = useState<CanvasPath[]>([]);

c…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vinothpandian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #119 on June 26, 2023 13:40.