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

react-view-models

v0.2.0

Published

Add Observable View-Models to React components

Downloads

5

Readme

React View-Models

Build Status Greenkeeper badge

Connect observable view-models to React presentational components to create auto rendering container components.

Install

ES6

import reactViewModel from 'react-view-models';
import { Component } from 'react-view-models';
import { makeReactComponent } from 'react-view-models';

CommonJS

var reactViewModel = require('react-view-models');
var Component = require('react-view-models').Component;
var makeReactComponent = require('react-view-models').makeReactComponent;

API

reactViewModel( displayName, ViewModel, Component )

Connect a ViewModel class to React presentational components and produce a renderer function (primarily for use in CanComponent.extend()).

reactViewModel() takes 3 arguments. The first (optional) is the displayName of the resulting ReactComponent (only used for render functions). The second is a ViewModel constructor function, which is an extended can-define/map. The third argument is a Presentational Component constructor function or render function. If Component is a render function, a new React Component will be created, extending CanReactComponent, which uses the provided render function. The reactViewModel() function returns a renderer function, which can be used by CanComponent.extend() or manually to create a DOM Fragment.

Since the Container Component doesn't produce DOM artifacts of it’s own, you won’t end up with any wrapper divs or anything to worry about, but in react-device-tools you will see the component with the displayName (or defaults to CanReactComponentWrapper) in the tree.

Example

var CanComponent = require('can-component');
var reactViewModel = require('react-view-models');
var stache = require('can-stache');

var ViewModel = DefineMap.extend('AppVM', {
  first: {
    type: 'string',
    value: 'foo'
  },
  last: {
    type: 'string',
    value: 'bar'
  },
  text: {
    get() {
      return this.first + this.last;
    },
  },
});

module.exports = CanComponent.extend({
  tag: 'app-component',
  ViewModel: ViewModel,
  view: reactViewModel('AppComponent', ViewModel, (props) => {
    return (
      <div>{props.text}</div>
    );
  })
});

Component class

Connect a ViewModel class to React presentational components

To use the Component class, your Presentational Component should extend Component instead of React.Component, and you should provide a static ViewModel on your class.

Every instance of the returned component will generate an instance of the ViewModel and provide it as props to the connected component. The ViewModel instance will be initialized with the props passed into the Container Component, and provided to your methods as this.props. Whenever the container component will receive new props, the new values are passed to the viewModels .set() method, which may in turn cause an observable change event, which will re-run the observed render process and provide the child component new props, which may cause a new render.

note: If you extend any of the react lifecycle methods, you must call super so as not to break the view-model binding. This includes: componentWillReceiveProps, componentWillMount, componentDidMount, componentWillUpdate, componentDidUpdate, componentWillUnmount

Example

import { Component } from 'react-view-models';
import DefineMap from 'can-define/map/';

export default class AppComponent extends Component {
  render() {
    return <div>{this.props.text}</div>;
  }
}

AppComponent.ViewModel = DefineMap.extend('AppVM', {
  first: {
    type: 'string',
    value: 'foo'
  },
  last: {
    type: 'string',
    value: 'bar'
  },
  text: {
    get() {
      return this.first + this.last;
    },
  },
});

makeReactComponent( displayName, CanComponent )

Convert a CanComponent class into a React Component.

makeReactComponent() takes 2 arguments. The first (optional) is the displayName of the ReactComponent. The second argument is a CanComponent constructor function. The makeReactComponent() function returns a React Component which can then be imported and used in any react component or render function as usual.

Since the Component doesn't produce DOM artifacts of it’s own, you won’t end up with any wrapper divs or anything to worry about, but in react-device-tools you will see the component with the displayName (or defaults to CanComponentWrapper) in the tree.

Example

import CanComponent from 'can-component';
import { makeReactComponent } from 'react-view-models';

const InnerComponent = makeReactComponent(
  CanComponent.extend('InnerComponent', {
    tag: 'inner-component',
    view: stache('<div class="inner">{{text}}</div>')
  })
);

export default class AppComponent extends Component {
  render() {
    return (
      <InnerComponent text="inner text" />
    );
  }
}

Common use cases when using a view model

Here are some examples that may come up when using a view-model that may not be obvious at first:

Transforming a prop before passing it down to a child component

Sometimes you want a prop that is set on your connected component to be set to the exact same prop key on the child component, but modified slightly before passing it down. Here's an example of that:

const ViewModel = DefineMap.extend({
  someProp: {
    set( newVal ) {
      return newVal.toUpperCase();
    }
  }
});

Calling a parents callback, while also doing something special in your view models callback

Sometimes, you still want to notify the connected components owner component that the state changed (by calling a callback), but only after or while doing something different within the view-model. In this case, you'll want to define the callback prop as a observable attribute with a getter, rather than a method, and use the lastSetVal argument to call the parent components callback.

const ViewModel = DefineMap.extend({
  onChange: {
    type: 'function',
    get( lastSetValue ) {
      return (ev) => {
        this.changeTheThing(ev.target);
        if ( lastSetValue ) {
          return lastSetValue(ev);
        }
      };
    }
  }
});

Contributing

Contributing

Running the tests

Tests can run in the browser by opening a webserver and visiting the test/test.html page. Automated tests that run the tests from the command line in Firefox can be run with

npm test

License

MIT