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)
}
}, [])
解决方案
我改变了调用动画的方式,所以现在我在 A Piece 组件中声明它并在其中的“useEffect”中启动它。
推荐阅读
- html - 使用 .each() 可调整大小的 jquery ui 仅适用于第一个孩子
- python - 比较两个没有重复的数据帧
- r - Highcharts/HighcharteR:在 R 中 50 多个系列之后,堆叠的柱形条变得很瘦
- python - 找不到烧瓶模板
- javascript - Vuejs - 观察嵌套对象更改不适用于动态对象创建
- java - AndroidRuntime:致命异常:java.lang.RuntimeException:无法实例化活动 ComponentInfo 在路径上找不到类:DexPathList
- java - 在数据库填充的 JTable 上应用过滤器
- asp.net-web-api - 使用 Snowflake 作为 Web API 的数据存储库有什么问题吗?
- php - pg_copy_from() 的输入的数组格式是什么?
- r - 基于匹配字符串或跨列子集的 NA 逐行变异