反应本机无限重投

时间:2020-07-04 12:39:54

标签: reactjs 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)
       }
     }, [])

1 个答案:

答案 0 :(得分:0)

我更改了动画的调用方式,因此现在我在A Piece组件中声明它,并在其中的'useEffect'中启动它。