reactjs - 使用 react-spring 在 react-use-gesture 中设置初始值
问题描述
我只是通过一些例子来学习 react-spring 和 react-use-gesture 。我不知道如何设置和存储 useSprings 的道具。试图设置初始值,所以它基本上是一个拖放。我意识到还有其他方法可以实现拖放,我正试图将我的注意力集中在 react-spring 上,这样我就可以开始构建其他东西了。
无论如何,使用该函数设置初始值会返回一个错误,即 pos 未定义。这是我的代码:
const [ props , set] = useSprings(4,(i) => ({ pos: [i*10, i*10] }))
const bind = useDrag(
({ args: [index], down, movement: pos }) => {
set((i) => {
if (index !== i) return
return { pos, immediate: down }
})
},
{ initial: () => pos.getValue() }
)
return props.map(({ pos }, i) => (
<animated.div
key={i}
className="Rocket"
{...bind(i)}
style={{ transform: interpolate([pos], ([x, y]) => `translate3d(${x}px,${y}px,0)`) }}
/>
))
}
如果我只有一个并使用 useSpring,我可以直接从钩子中设置初始值:
const [{ pos }, setPos] = useSpring(() => ({ pos: [0, 0] }))
const bind = useDrag(
({ down, movement: pos }) => {
setPos({ pos, immediate: down })
},
{ initial: () => pos.getValue() }
)
return (
<animated.div
className="Rocket"
{...bind()}
style={{ transform: interpolate([pos], ([x, y]) => `translate3d(${x}px,${y}px,0)`) }}
/>
)
}
我似乎无法追踪它。我有点同时学习钩子,所以我对钩子的存储方式和位置的理解可能是我的问题。非常感谢任何见解。
解决方案
推荐阅读
- java - 如何将 Java 日期转换为 HTTP If-Modified-Since 日期格式?
- python - 线程生成输出为 None 并在 Python 中产生相同的结果
- c# - 检查列表中的位置是否相同
- matlab - 连接散点图
- apache-kafka - 当我从 kafka 轮询多个分区的记录并在一个分区上提交一条记录时,其余记录都丢失了
- azure-cognitive-search - 选择满足谓词的嵌套对象
- c# - .NET 2.0 中的等效任务是什么?
- java - Proguard 删除 com.sun.mail.imap.IMAPProvider
- javascript - How to catch google chrome audit lighthouse
- ios - Safari / Chrome / Firefox for iOS 中的正文和固定的 div 滚动问题