GithubHelp home page GithubHelp logo

Comments (4)

mjackson avatar mjackson commented on April 24, 2024

Transition hooks were never intended to be called on the initial create. For example, if you return false to cancel the initial transition, where does that leave us?

Think of transition hooks like the componentWillUpdate React component lifecycle method.

from history.

aldendaniels avatar aldendaniels commented on April 24, 2024

Transition hooks were never intended to be called on the initial create.

Yes, but I'm suggesting that doing so now would be a good idea. πŸ˜„

For example, if you return false to cancel the initial transition, where does that leave us?

Abort (never call listeners) and log an error or warning to the console.

This feature is a must have if we want to allow users to use transition hooks to block transitions during async data loading. I've tried to make the case for why I think this is important. I'm guessing, however, that we still aren't on one page about whether or not the use-case is legitimate. Until we agree on the use-case, we're not going to get very far with the implementation discussion.

from history.

mjackson avatar mjackson commented on April 24, 2024

This feature is a must have if we want to allow users to use transition hooks to block transitions during async data loading

I don't agree. You already have an API to block transitions. What you're looking for is a way to block the initial "transition" while you load data, which you could easily do with a history enhancer:

function useDataLoader(createHistory) {
  return function (options={}) {
    let { dataLoader, ...historyOptions } = options
    let history = createHistory(historyOptions)

    let prevLocation = null

    function listen(listener) {
      return history.listen(function (location) {
        if (prevLocation == null) {
          // Block the initial transition and load some data.
          // loadData is an API that asynchronously loads some
          // data for a given location.
          dataLoader.loadData(location, function () {
            // getData is a synchronous method that gets previously
            // loaded data from a client-side cache.
            listener(location, dataLoader.getData(location))
          })
        } else {
          // Data was already loaded in the listenBefore callback.
          listener(location, dataLoader.getData(location))
        }

        // Hang onto the previous location to know if
        // we're in the initial listen callback or not.
        prevLocation = location
      })
    }

    // Use a listenBefore hook to block subsequent
    // transitions while we load data.
    history.listenBefore(function (location, callback) {
      dataLoader.loadData(location, callback)
    })

    return {
      ...history,
      listen
    }
  }
}

This isn't really that complex, and it means that we don't have to make assumptions in the history package about how people want to do their data loading.

from history.

mjackson avatar mjackson commented on April 24, 2024

Closing this, as the conversation has stalled. Hopefully the code I posted above helps.

from history.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.