embiem / react-canvas-draw

React Component for drawing in canvas

Home Page:https://embiem.github.io/react-canvas-draw/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use references????

micTrb opened this issue · comments

I'm trying to use the funcionalities like undo(), clear() etc...
I've followed the example provided but I really ddon't understand

<CanvasDraw
ref={canvasDraw => (this.saveableCanvas = canvasDraw)}
/>

that's what I've done in my code, but it seems that ref is not undefined....

Someone can please explain me better how to use functionalities and refs?

If you're using React Hooks try this :

import {useRef} from 'react';

const YourComponent = () => {
    const canvas = useRef();
    return(
        <>
            <CanvasDraw ref={canvas}/>
            <button onClick={() => canvas.current.clear()}>Clear canvas</button>
        </>
    )
};

If you're still using classes :

class YourComponent extends React.Component {
    constructor(props) {
        super(props);
        this.canvas = React.createRef();
    }
    render() {
        return(
            <>
                <CanvasDraw ref={this.canvas}/>
                <button onClick={() => this.canvas.current.clear()}>Clear canvas</button>
            </>
        )
    }
}

More informations about refs here.

Hello Michele,

I wasn't able to reproduce your issue, can you provide a live working example ?
What are you passing inside defaultProps ?

You're mixing up hooks and classes, I would advise you to choose either one or another : useRef() or createRef().

Anyway, this is not an issue related to the component, you can close this issue :)

I would recommend to put these examples in the README. Given example is not using useRef nor .current.