zyestin/status-sync-global/ #2
Replies: 1 comment
-
Good ! giscus was added successfully! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
zyestin/status-sync-global/
有这么一个常见的需求 在对某个帖子进行点赞后,该帖子所出现的各个地方,e.g.帖子列表(可能多处)、帖子详情(可能多个),点赞状态实时同步、数量加/减1同步。
iOS 方案 在iOS,我是在每个帖子 所对应的 Model(数据)对象里 监听通知,判断 帖子ID,ID匹配 则更新 点赞状态,以此达到操作点赞后 实时同步同一帖子的点赞状态,非常简单好使!
RN 方案 但RN里,数据是一个{id:xx, ...}这样的对象,是不支持像在iOS那样,在这样对象里 写监听通知的代码的。
于是,那就放到hooks去实现。
Q:那需要什么样的hooks呢? A:也就是需要怎样的 设计最优雅的API,or调用方式 (如下),才是决定了设计什么样的hooks函数
最优雅API 调用方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const PostComp = () => { const { isActive: liked, activeCount: likeCount, changeActiveStatus: changeLikeStatus, } = useActiveStatus({ id: id, originalActiveCount: originalLikeCount, originalActiveStatus: originalLikedStatus, notificationKey: kPostLikeStatusChangeNotification, requestFunc: ({ isActive }) => { return likePost({ postId: id, isLike: isActive }); // send request to toggle liked status }, }); return ( {likeCount} <Button onPress={() => { changeLikeStatus() }} /> ) } useActiveStatus这个hooks的实现源码, 如下
https://zyestin.github.io/zyestin/status-sync-global/
Beta Was this translation helpful? Give feedback.
All reactions