Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 我能';我无法得到画布的宽度_Reactjs - Fatal编程技术网

Reactjs 我能';我无法得到画布的宽度

Reactjs 我能';我无法得到画布的宽度,reactjs,Reactjs,在第一行中,您可以看到react能够从DOM获取canvas元素(通过ref),但是第二行和第三行的输出是错误的!我无法获得正确的宽度,这令人困惑。。。出什么事了 试试getBoundingClientRect() 假设您将ref属性作为“mycanvas”赋予canvas元素,则可以执行this.refs.mycanvas.getBoundingClientRect().width事件。target为您提供本机DOM节点,然后您需要使用常规DOM API访问属性。您可以执行event.targ


在第一行中,您可以看到react能够从DOM获取canvas元素(通过
ref
),但是第二行和第三行的输出是错误的!我无法获得正确的宽度,这令人困惑。。。出什么事了

试试
getBoundingClientRect()


假设您将ref属性作为“mycanvas”赋予canvas元素,则可以执行
this.refs.mycanvas.getBoundingClientRect().width
事件。target
为您提供本机DOM节点,然后您需要使用常规DOM API访问属性。您可以执行
event.target.dataset.tag
event.target.getAttribute('data-tag')
;任何一个都可以

console.log(canvas);   
console.log(canvas.width);  
console.log(canvas.getAttribute("width"));