• Stars
    star
    196
  • Rank 197,415 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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).

Screenshot

More Repositories

1

es-css-modules

PostCSS plugin that combines CSS Modules and ES Imports
JavaScript
170
star
2

state-transitions

React animation
JavaScript
119
star
3

cssta

TypeScript
97
star
4

react-native-tableview-list

TypeScript
59
star
5

react-isomophic-demo

JavaScript
53
star
6

react-twentytwenty

A TwentyTwenty component for React
JavaScript
49
star
7

node-gcm-ccs

Google Cloud Messaging via XMPP (CCS)
JavaScript
34
star
8

jacob-does-code

MDX
30
star
9

react-native-web-worker

Objective-C++
21
star
10

redux-enqueue

Simple queue system for redux
JavaScript
14
star
11

babel-plugin-swift-blocks

Swift-style blocks for JS
JavaScript
14
star
12

recora

JavaScript
14
star
13

technicalc-core

ReScript
13
star
14

postcss-remove-classes

JavaScript
12
star
15

css-class-generator

JavaScript
11
star
16

reasonml-playground-native

JavaScript
9
star
17

react-native-drag-drop-ios

Objective-C
9
star
18

react-native-cloudkit-storage

Objective-C
7
star
19

json-view

JSON viewer
HTML
7
star
20

tree-shaking-bootstrap

CSS
6
star
21

react-native-scroll-to-enhanced

Enhanced scrollTo functionality for scrollviews in React Native
JavaScript
6
star
22

babel-plugin-react-displayname-path

JavaScript
5
star
23

react-native-screenshot-tools

JavaScript
5
star
24

js-code-stats

JavaScript
5
star
25

recora-web

JavaScript
4
star
26

react-native-uistepper

Java
3
star
27

react-global-component-stack

JavaScript
3
star
28

Multilayout-Bootstrap

JavaScript
3
star
29

react-native-scrollintoview

Java
3
star
30

Theta-Swift

Swift
3
star
31

recora-old

JavaScript
2
star
32

middleware-all-the-way-down

JavaScript
2
star
33

color-forge

Simple Color System
JavaScript
2
star
34

react-native-sfimage

Java
2
star
35

react-native-accessibility-settings

Java
2
star
36

post-iconfonts

Converts an icon font to an SVG image
JavaScript
2
star
37

postcss-transform-classes

JavaScript
1
star
38

piano

TypeScript
1
star
39

es-css-modules-demo

JavaScript
1
star
40

technicalc-calculator

1
star
41

babel-plugin-pre-evaluate-template-strings

JavaScript
1
star
42

webpack-superalias

JavaScript
1
star
43

babel-plugin-cssta

JavaScript
1
star
44

InfiniteCanvas

Swift
1
star