反应很棒,但是当使用Javascript(好像你有)时,理解和难以调整(从经验)可能有点棘手。在React上有一个很棒的YouTube教程learncodeacademy,它真的可以帮助你understand react。您可能还想看看create react app以便设置React项目。
现在你的问题:)这不是一个好的做法。 React有自己的“window.onload”,叫做componentDidMount
另外,除非绝对必要,否则你不应该使用getElementBy。
反应的美丽之处在于使用状态。
你的CSS值应该是一个状态。
这方面的一个例子是:
import React, { Component } from 'react'
import MyComponent from './MyComponent'
class Animation extends Component {
constructor() {
super() //this always has to be called first in a constructor
this.state = {
animationCSS: '',
}
this.changeAnimationCSS = this.changeAnimationCSS.bind(this)
}
componentDidMount() {
const getAnimationCSSFromDB = db.get(animationCSS) //This obviously does not work, but an example
this.setState({
animationCSS: getAnimationCSSFromDB
})
}
changeAnimationCSS() {
this.setState({
animationCSS: //Whatever new css you may want
})
}
render() {
return (
<MyComponent propertyForStylingAnimation={this.state.animationCSS} />
<button onClick={this.changeAnimationCSS} label="Button" />
)
}
}
export default Animation
MyComponent的可能在这种情况下,像这样
import React from 'react'
const MyComponent = props =>
<div style={{ animate: props.propertyForStylingAnimation }}> // This does not work for animating since animate is not a css property.
// Stuff
</div>
export default MyComponent
了解props可以是一个有点棘手,但如果按照YouTube的教程由learncodeacademy,你会得到它。
请注意,第二位代码要短得多。
这是因为它是stateless。这意味着什么只是没有国家。
这意味着我不需要定义扩展组件的类,我可以只使用一个常量。我也不需要定义渲染或返回,因为只有一个元素(div)被返回,不需要括号。在学习React时,这并不是您最初需要担心的事情,但这是一种很好的做法。
为什么不把你的动画放在css类上? –