question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lifecycle events as attributes?

See original GitHub issue

I’ve been thinking about this a bit lately. The only reason you would ever need a class-based component (with a redux-style architecture) is if you need the lifecycle events.

The lifecycle events seem awfully similar to the onClick, onMouseDown event handlers, so I’m wondering if it would be possible to include those events like onMount as attributes on the vnodes?

Maybe they would only work on the component level, but with mutation observers, I think they could work on any element. Here’s some repos I’ve used in the past for the mutation events:

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
matthewmuellercommented, Oct 6, 2016

for what it’s worth, this is what I settled on for functional components:

  return function mounts (node) {
    let attrs = node.attributes
    if (!attrs) return
    else if (!attrs.onMount && !attrs.onUnmount) return

    // create a mount using the ref
    let ref = node.attributes && node.attributes.ref

    node.attributes.ref = (el, send) => {
      const parent = el && el.parentNode

      if (attrs.onMount && el && !parent) {
        // when initially mounted, el will
        // exist but there won't be a parent.
        // we want to defer to ensure that
        // the dimensions have been calculated
        defer(function () { attrs.onMount(el, send) })
      } else if (attrs.onUnmount && !el && !parent) {
        // when unmounting, both el and
        // the parent will be null.
        defer(function () { attrs.onUnmount(null, send) })
      }

      // call original ref, if there is one
      ref && ref(el, send)
    }
  }

The problem I ran into with the previous approaches in the functional case was that inst would get reset and onMount would get called every re-render. By checking the actual element, it will only get called when the element is added and removed.

UPDATE: You’ll also need to add a check for the server. Here’s the complete example: https://github.com/matthewmueller/vcom/blob/2cbffdc7fe4ac2e10c9527c3cb9a0b4541333c7e/index.js#L179-L211

2reactions
developitcommented, Aug 25, 2016

@matthewmueller also: I wonder if it would be better to just steal the corresponding lifecycle method names from the Web Components spec?

<div
  createdCallback={ () => console.log('instantiated') }
  attachedCallback={ () => console.log('mounted') }
  detachedCallback={ () => console.log('unmounted') }
/>

Then we could really start to look into making Preact a mechanism for building and shipping Web Components…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lifecycle event properties
Lifecycle event properties control the behavior of lifecycle events. You can set the duration of the activity set closure time in hours.
Read more >
How do JavaScript Page Lifecycle Events work? - Tilo Mitra
The lifecycle of an HTML page has three important events: ... Attributes async and defer work only for external scripts.
Read more >
How To Create Lifecycle Events
Lifecycle events can be configured to run based on events that occur in IdentityIQ. For example, when a manager change is detected for...
Read more >
Lifecycle hooks - Angular
A component instance has a lifecycle that starts when Angular instantiates the component class and renders the component view along with its child...
Read more >
Screen and Block Lifecycle Events - OutSystems documentation
Screens and Blocks follow a lifecycle composed by a set of stages. Learn what those stages are and you what can do at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found