npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

re-angular-dux

v1.0.3

Published

Redux connector for angular based on react props

Downloads

2

Readme

re-angular-dux

Simple angular connector for Redux, based on the props object from React.

There are several great Redux connectors for Angular 2+ out there, and most of them uses the Observables pattern from RxJS which makes a lot of sense. However, many times you just want the newest state available and you don't want to have to worry about observable sequences and asynchronous data. Enter re-angular-dux (React-Angular-Redux - GET IT?).

You basically connect the constructor of your component with the Redux store, and define which part of the store you're interested in with the InputMapper and off you go. All of this is typesafe. See an example below:

import { Connect, InputMapper } from 're-angular-dux'

// Assuming we have a store class called AppState which looks like this
class AppState {
    helloWorldState = {
        hello = "Hello",
        world = "world"
    }
}

// In the component you want to connect, start by declaring which properties you want (this.state is automagically set by re-angular-dux)
class Input extends InputMapper<AppState> {
    hello = this.state.helloWorldState.hello;
    world = this.state.helloWorldState.world;
}


@Component({
    selector: 'hello-world-connected-component',
    template: `
        {{ props.hello }}, {{ props.world }}!
    `
})
// This line is important. It Connects (duh) your component to the store using the input mapper we just built.
class HelloWorldConnectedComponent extends Connect(Input) { 
    constructor(changeDetectorRef: ChangeDetectorRef) {
        super(changeDetectorRef)
    }

    // All mapped properties are available with type safety
    someMethod() {
        alert(this.props.hello + ', ' + this.props.world);
    }
}

That's great and all... How do I get it?

Quite right! Re-angular-dux is very easy to set up, since it sidesteps angulars dependency injector. This might get changed at a later date, but for now the store reference is just a static value that needs to be set on NgReduxComponent. A complete setup example below:

import { createStore } from 'redux'
import { NgReduxComponent } from 're-angular-dux'

// Setup your redux store with reducer, default state and middleware
const store = createStore<AppState>();

// Make your newly created store available to re-angular-dux
NgReduxComponent.store = store;

And that's it! Your angular components can now connect to Redux. If you're using redux and re-angular-dux everywhere in your application, I recommend telling angular to use the push change detection strategy. Since we're manually telling angular to update, we can tell it to stop listening for changes everywhere. Do this in your outermost component.

    @Component({
        ...
        changeDetection: ChangeDetectionStrategy.OnPush,
        ...
    })
    class MyAppComponent {}

State mixins

So say you have your store set up in a way, that a subset of whatever state you have matches the needs of one of your connected components. Enter state mixins! Let's start with the example from before:

import { Connect, InputMapper } from 're-angular-dux'

class HelloWorldState {
    hello = "Hello";
    world = "World";
}
class AppState {
    helloWorldState = new HelloWorldState();
}

// Now instead of selecting invidiual properties from the state
// lets mixin the HelloWorldState
class Input extends InputMapper<AppState> {
    constructor(state: AppState) {
        super(state, state.helloWorldState);
    }
}

@Component({
    selector: 'hello-world-connected-component',
    template: `
        {{ props.hello }}, {{ props.world }}!
    `
})
// When we connect, let's tell re-angular-dux which state we're mixin in, for type safety's sake
class HelloWorldConnectedComponent extends Connect(Input, HelloWorldState) { 
    constructor(changeDetectorRef: ChangeDetectorRef) {
        super(changeDetectorRef)
    }

    // All mapped properties are available with type safety
    someMethod() {
        alert(this.props.hello + ', ' + this.props.world);
    }
}

So now even large complex state trees can be mixed in, without having to change a lot of code. Note that you can still pick out properties from other parts of the store.

A word on ngOnChanges

Because of the way re-angular-dux works right now, ngOnChanges does not get fired on connected components. It'll still work on any components in the tree rendered by a connected component, just not the one with the Connect() directly attached to it's constructor... Which leads us to...

TODO

Stuff that would be nice to have in this package:

  • ngOnChanges support.
  • Test suite with Travis integration