How to access canvas context in React

后端 未结 6 1107
迷失自我
迷失自我 2021-01-11 11:26

I made a color picker with React and Canvas. Currently the components are rendered in React and canvas is done with vanilla javascript. I\'d like to two to mesh more, so I w

6条回答
  •  暖寄归人
    2021-01-11 11:52

    In accordance to React16 You can use React.createRef()

    class ColorPicker extends React.Component {
    constructor(props) {
       super(props);
    
       this.colorPickerRef = React.createRef();
    }
    
    componentDidMount() {
       this.context = this.colorPickerRef.current.getContext('2d');
    }
    
    render() {
       return (
          
       )
    }
    }
    

提交回复
热议问题