Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-hooks

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-hooks

Vue composition functions in Angular

  • 0.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

angular-hooks

Use Vue Function API in Angular.

Warning: This is currently still experimental and unstable.

TODO

  • state function & unwrapping of wrappers in template
  • add options watch to choose update mode:
    • sync: call watch handler synchroneously when a dependency has changed.
    • pre: call watch handler before rerendering
    • post: call watch handler after rerendering

Install

  1. Make sure Angular v6 or higher is installed.

  2. Make sure RxJs v6 or higher is installed.

  3. Install module: npm install angular-hooks --save

Usage

To use Angular Hooks, you need to first let your component inherit UseHooks<T> with T being your component. This allows us to add the necessary logic and typing to the component before it is executed.

To finish, you only need to add the ngHooks method to your component. The return value of this function will automatically be exposed on this.$data.

Restrictions

  • ngHooks needs to be synchroneous.
  • All functions presented below are only available in ngHooks.

Features

For now, there is no advanced description available for each function. Each function therefore only links to an example using the feature.

Observables wrappers:

Automatic subscribe / unsuscribe:

  • subscribe

Injector:

Dynamic lifecycles:

Example

Setup

value returns a new Wrapper with the given initial value.

import { value, UseHooks } from 'angular-hooks'

@Component({
  // ...
})
export class MyComponent extends UseHooks<MyComponent> {

  ngHooks() {
    const counter = value(0);

    return {
      counter
    };
  }
}
<p>{{ $data.counter.value }}</p>

Reactive inputs

observe turns a property on the given object into a reactive Wrapper. computed automatically recomputes it's value if one of it's dependencies has changed. It will also only recompute it's value when needed.

import { observe, computed, UseHooks } from 'angular-hooks'

@Component({
  // ...
})
export class MyComponent extends UseHooks<MyComponent> {
  @Input()
  title: string = "Hello world";

  ngHooks() {
    const title = observe(this, props => props.title);
    const reversedTitle = computed(() => {
      return title.value
        .split('')
        .reverse()
        .join('');
    })

    return {
      title,
      reversedTitle
    };
  }
}
<h1>{{ $data.title.value }}</h1>
<h2>{{ $data.reversedTitle.value }}</h2>

Watch route

provide makes use of Angulars Injector to get the appropriate provider. fromObservable turns an RxJs observable into a Wrapper. asObservable turns a Wrapper into an RxJs observable. watch observes a Wrapper and triggers the handler each time the Wrapper changes. Automatically unsubscribes when the component is destroyed.

import { provide, watch, fromObservable, UseHooks } from 'angular-hooks'
import { ActivatedRoute } from '@angular/router';

function useRoute() {
  const route = provide(ActivatedRoute);
  const params = fromObservable(router.params);
  return {
    params
  }
}

@Component({
  // ...
})
export class MyComponent extends UseHooks<MyComponent> {

  ngHooks() {
    const route = useRoute();
    const id = computed(() => route.params.value.id);

    const todo = value<any>(undefined);

    watch(id, async (value) => {
      const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${value}`);
      todo.value = await res.json();
    })

    return {
      id,
      todo
    };
  }
}
<h1>{{ $data.id.value }}</h1>
<div *ngIf="$data.todo.value">
  <p>{{ $data.todo.value.title }}</p>
</div>

Dynamic lifecycle hooks

import { onInit, onDestroy, value } from 'angular-hooks'

function useMouse() {
  const x = value(0);
  const y = value(0);

  const update = (e: MouseEvent) => {
    x.value = e.pageX;
    y.value = e.pageY;
  };

  onInit(() => {
    window.addEventListener("mousemove", update, false);
  });
  onDestroy(() => {
    window.removeEventListener("mousemove", update, false);
  });

  return {
    x,
    y
  };
}

@Component({
  // ...
})
export class MyComponent extends UseHooks<MyComponent> {

  ngHooks() {
    return {
      ...useMouse()
    };
  }
}
<p>{{ $data.x.value }} - {{ $data.y.value }}</p>

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Keywords

FAQs

Package last updated on 02 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc