【发布时间】:2018-04-10 13:40:00
【问题描述】:
我是原生反应新手,想问一些问题。首先对不起,如果他们是愚蠢的。 :(
我一直在观看并尝试通过一些视频课程学习 React Native,我发现了这个 https://blog.expo.io/introducing-expo-ar-mobile-augmented-reality-with-javascript-powered-by-arkit-b0d5a02ff23 我冲浪时的指南。我复制并粘贴了代码,并尝试通过删除一些行来了解它们在做什么。当我删除“参考”线相机没有工作。我真的不明白为什么会这样。所以决定在这里问。问的时候,我认为问其他问题会很好。那么问题来了:
什么是 ref,什么时候用,什么时候不用?
什么是构造函数,什么时候用,什么时候不用?
已经感谢您的回答了!
代码是:
`
import Expo from 'expo';
import React from 'react';
import * as THREE from 'three'; // 0.87.1
import ExpoTHREE from 'expo-three'; // 2.0.2
console.disableYellowBox = true;
export default class App extends React.Component {
render() {
return (
<Expo.GLView
ref={(ref) => this._glView = ref}
style={{ flex: 1 }}
onContextCreate={this._onGLContextCreate}
/>
);
}
_onGLContextCreate = async (gl) => {
const width = gl.drawingBufferWidth;
const height = gl.drawingBufferHeight;
const arSession = await this._glView.startARSessionAsync();
const scene = new THREE.Scene();
const camera = ExpoTHREE.createARCamera(arSession, width, height, 0.01, 1000);
const renderer = ExpoTHREE.createRenderer({ gl });
renderer.setSize(gl.drawingBufferWidth, gl.drawingBufferHeight);
scene.background = ExpoTHREE.createARBackgroundTexture(arSession, renderer);
// Edit the box dimensions here and see changes immediately!
const geometry = new THREE.BoxGeometry(0.07, 0.07, 0.07);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
cube.position.z = -0.4;
scene.add(cube);
const animate = () => {
requestAnimationFrame(animate);
cube.rotation.x += 0.07;
cube.rotation.y += 0.04;
renderer.render(scene, camera);
gl.endFrameEXP();
}
animate();
}
}
`
【问题讨论】:
-
嗨,约翰,欢迎来到 StackOverflow!尝试在实际问题中包含一些代码,这样人们就不需要采取额外的步骤来查看您所指的代码。据我回忆,react 中的构造函数就像其他语言中的构造函数一样——你可以使用它们来执行实例化过程中所需的任务。至于
ref是什么:你很可能在官方文档中找到这样的答案:reactjs.org/docs/refs-and-the-dom.html。祝你学习顺利:) -
@WilliamPatton 感谢您的热烈欢迎和快速答复!我更新了我的问题:)
标签: javascript react-native constructor components ref