百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

hooks-test

> 编程语言
开源

一个相对复杂的用例通过 React Hooks 解决得相当不错。请注意,这是 hooks 的 alpha 版本,还有一些需要注意的地方…

194 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个相对复杂的用例通过 React Hooks 解决得相当不错。请注意,这是 hooks 的 alpha 版本,还有一些需要注意的地方…

A somewhat complicated use case solved relatively nicely with React Hooks. Note this was the alpha version of hooks - there's a few things that need to be updated for the hooks that were actually released stuff needs updating to the release version of hooks.

One of my companies had an app made with React, but part of that was a canvas. The canvas was very complicated, and was essentially its own app. The main parts of complexity were:

  • Keyboard handlers
  • Pan and zoom
  • Momentum when panning (aka slippy maps, depends on above)
  • Layout animations
  • Coordinating layout animations with map position (keep the current node in the same position when the layout changes)
  • Drag and drop between all combinations of the canvas and React

Using component composition, I was able to split the logic into a few components parts: a component that handled panning and zooming, a component that drew on a canvas, a keyboard event handler component. However, I was unable to split it much further, and the first two components were about 1,000 lines each, as they had to handle so much.

So I tried again with hooks! It's. So. Much. Simpler. In total, we're getting close to the previous 2,000 lines of tightly coupled logic. However, each file never goes much above 50 lines, and it is much easier to follow how things fit together.

It is possible to incorporate existing HOC code, as demonstrated with react-dnd. There seemed to be a bug where useImperativeMethods didn't seem to work unless I wrapped my component in a useless class wrapper. We also end up drilling down with getDecoratedComponentInstance to get the correct instance. This more exposes issues with the pre-existing patterns, and hooks should completely alleviate these problems when libraries upgrade.

I thought it would be cool to share to share concepts on how to compose use directives to achieve something like this.

If you want to read about how I found it, I replied in the RFC over here.

Initialized with Create React App (run npm start to begin).

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言