Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

How to use references???? #104

Closed
micTrb opened this issue Dec 9, 2020 · 4 comments
Closed

How to use references???? #104

micTrb opened this issue Dec 9, 2020 · 4 comments
Labels
question Further information is requested

Comments

@micTrb
Copy link

micTrb commented Dec 9, 2020

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?

@SimonBellucci
Copy link

SimonBellucci commented Dec 9, 2020

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.

@micTrb
Copy link
Author

micTrb commented Dec 10, 2020 via email

@SimonBellucci
Copy link

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 :)

@Talmaj
Copy link

Talmaj commented Jun 15, 2021

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

@embiem embiem added the question Further information is requested label Nov 7, 2021
@embiem embiem mentioned this issue Nov 7, 2021
2 tasks
@embiem embiem closed this as completed Nov 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants