我刚刚发现,在this.setState()任何组件中的react 函数是异步的,或者在调用它的函数完成后调用.
现在我搜索并找到了这个博客(http://www.bennadel.com/blog/2893-setstate-state-mutation-operation-may-be-synchronous-in-reactjs.htm)
在这里,他发现setState异步(在堆栈为空时调用)或同步(调用后立即调用)取决于触发状态变化的方式.
现在这两件事很难消化
setState函数在函数内部被调用updateState,但触发updateState函数的内容并不是被调用函数所知道的.setState像JS一样异步是单线程语言,这个setState不是WebAPI或服务器调用,所以必须只在JS的线程上完成.他们是这样做的,以便重新渲染不会停止所有事件监听器和东西,或者存在其他一些设计问题.如何在React中使用jQuery UI?我在Googling看过几个例子,但所有这些例子似乎都已经过时了.
我正在阅读该问题的最佳答案。
有人提供了在react中使用threejs的一个很好的例子:
import React, { Component } from 'react'
import * as THREE from 'three'
class Scene extends Component {
constructor(props) {
super(props)
this.start = this.start.bind(this)
this.stop = this.stop.bind(this)
this.animate = this.animate.bind(this)
}
componentDidMount() {
const width = this.mount.clientWidth
const height = this.mount.clientHeight
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(
75,
width / height,
0.1,
1000
)
const renderer = new THREE.WebGLRenderer({ antialias: true })
const geometry = new THREE.BoxGeometry(1, 1, 1)
const material = new …Run Code Online (Sandbox Code Playgroud)