Skip to content

RFC / useTransition 🤯 and possible improvements #665

@drcmda

Description

@drcmda

As we all know, transitions are hard. 🤯

If you want put your ideas in here and we'll discuss them, no matter how wild they are.

What people don't easily understand is that transition is a value retainer, it's not magic, it just saves a value (or an array of values) and looks for the addition or removal of keys. On changes it can retain the old value and pass it to the view, so that it can fade out an old state. So that has to be expressed better in the docs for sure. But the api is a little weird, too.

  1. A transition result is always an array. In theory this probably cannot change since there are reasons for it, it has to be documented better. Maybe it can be abstracted, though.

  2. That array contains objects made of { item, key props }, which is weird. Often people see "item" down in the view section and don't know where it comes from. This can be changed.

Some ideas:

// Proposed shortcut signature, if items are atomic (as in, items serve as keys)
const items = ['each', 'item', 'can', 'serve', 'as', 'a', 'key']
useTransition(items, config)

// When items aren't atomic it needs a key-accessor to recognize items
const items = [{ id: 0, text: 'hello' }, { id: 1, text: 'world' }]
useTransition(items, item => item.id, config)

// Function abstraction?
// Shorter, saves user from worrying about keys, retained item can be named properly
const transition = useTransition(visible, { from: {}, enter: {}, leave: {} })
return transition((visible, style) => visible && <a.div style={style}>hello</a.div>)

The current solution:

const transitions = useTransition(visible, null, { from: {}, enter: {}, leave: {} })
return transitions.map(
  ({ item: visible, props, key }) =>
    visible && (
      <a.div key={key} style={props}>
        hello
      </a.div>
    )
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: requestNew feature or request that should be actioned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions