繁体   English   中英

React-native 无限重新渲染

[英]React-native infinite re-renders

我正在构建 2048 游戏的副本。

我现在正在处理动画,当使用任何类型的数组更新 state 时,由于某种原因,我似乎得到了无限的重新渲染。

似乎将数组以外的任何内容传递给“setAppearAnimations”都可以正常工作。

提前致谢:)

const GameProvider = (props) => {
  const [appearAnimations, setAppearAnimations] = useState([])

  const addAppearAnimation = (animation) => {
    const newAppearAnimations = appearAnimations.concat([animation])
    console.log(newAppearAnimations)
    setAppearAnimations(newAppearAnimations)
  }
const Piece = ({ piece }) => {      
   const { value, prevX, prevY, x, y, hasJustAppeared } = piece      
   let appearAnimation = 1
   useEffect(() => {
       //Add appear animation       
       if (hasJustAppeared) {
           appearAnimation = new Animated.Value(0)         
           addAppearAnimation(appearAnimation)
       }
     }, [])

我改变了调用动画的方式,所以现在我在 A Piece 组件中声明它并在其中的“useEffect”中启动它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM