首页 > 解决方案 > React-native 无限重新渲染

问题描述

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

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

似乎将数组以外的任何内容传递给“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)
       }
     }, [])

标签: reactjsreact-native

解决方案


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


推荐阅读