【发布时间】:2021-08-16 22:12:49
【问题描述】:
问题。 每次我尝试使用 useRef 时,它都会抛出一个错误 Uncaught TypeError: Object(...) is not a function。
我正在尝试使用 react 钩子制作一个threejs 场景,但总是出现此错误。但在过去我使用基于类的组件完成它,它工作正常
这里是代码
const Updated = () => {
var earthMesh, freedomMesh;
var scene;
var result;
const mount = useRef(null); //error here
const controls = useRef(null);
const createMe = () => {
const width = mount.current.clientWidth;
const height = mount.current.clientHeight;
var scene = new THREE.Scene();
scene.background = new THREE.Color(0xdddddd);
const camera = new THREE.PerspectiveCamera(60, width / height, 1, 5000);
camera.position.set(0, 25, 25);
};
useEffect(() => {
createMe();
}, []);
return (
<div>
<div
style={{
width: "600px",
height: "470px",
}}
ref={mount}
/>
</div>
);
};
export default Updated;
【问题讨论】:
-
你的反应版本是什么?和反应域?
-
我只是检查它是一个旧的。它的 16.5.2,我如何也用其他软件包升级它?
-
你是如何导入
useRef的?
标签: javascript reactjs react-hooks