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

choo-component

v0.2.2

Published

A super helpful library to imitate reusable stateful components in Choo.

Downloads

4

Readme

Choo component

A super helpful library to imitate reusable stateful components in Choo.

Usage

Creating a component

// titleComponent.js
const component = require('./index')
const titleComponent = component({
  model: {
    namespace: 'title',
    state: {
      title: 'Default title'
    },
    reducers: {
      updateTitle (state, payload) {
        return {
          title: payload.title
        }
      }
    }
  },

  render (props) {
    return html`
      <div>
        <input
          value=${props.title}
          oninput=${(e) => {
            props.somethingHappened(e.target.value)
            props.updateTitle({
              title: e.target.value
            })
          }}
        >
        ${props.title}
      </div>
    `
  }
})

A component's definition must include a model which is an object containing the following.

  1. namespace (required) - a unique namespace to store instances of this component in your choo global state
  2. state - default state for an instance of a component
  3. reducers (required) - an object containing methods to update the internal state of the component. Note that reducers receive current instance's state in it's first argument, and must return updated state for the instance
  4. effects - an object containing effects. You will notice that the payload for effects will have an instanceId key. Don't worry about it, this is used internally to figure out which component you are updating

A component's definition must also include a view. The view receives a props object that contains a flattened object of the following:

  1. Current state of the instance
  2. Methods that correspond to reducer methods
  3. Props passed from the parent component

When calling reducers from the view, you must pass an object as the payload or bad things will happen!

The above example will export an object containing model and a component. See the sections below for useage.

Using a component in your app

First, register the model in your app.

// app.js
const titleModel = require('./titleComponent').model
app.model(titleModel)

For views, you first need to instantiate the component by passing state, prev and send to it. Next, create an instance of a component by calling the result of the instantiation. An instance expects the following:

  1. A unique identifier for the component. This needs to be completely unique, and should not change between re-renders of your view. It's how the component knows which slice of state it belongs to
  2. An optional object containing the following: 2.1 props - external properties you would like to propagate to your components view. This is useful for callbacks or component customisation 2.2 initialState - this is generally not used, but can be used to override state from the model during the setup stage of a component. Alternatively, you can manually call the reducer from outside your component to achieve the same result
// app.js
const html = require('choo/html')
const titleComponent = require('./titleComponent').component
const mainView = (state, prev, send) => {
  const title = titleComponent.component(state, prev, send)

  return html`
    <div>
      <h2>Title component demo</h2>
      ${title('one', {
        props: {
          somethingHappened (title) {
            console.log('Title one updated', title)
          }
        }
      })}
      <br />
      ${title('two', {
        props: {
          somethingHappened (title) {
            console.log('Title two updated', title)
          }
        },
        initialState: {
          title: 'Easy!'
        }
      })}
    </div>
  `
}

Note that your component must be initiated in a page that has access to state, prev and send

Altogether now

const choo = require('choo')
const html = require('choo/html')
const component = require('./index')
const app = choo()

const titleComponent = component({
  model: {
    namespace: 'title',

    state: {
      title: 'Default title'
    },

    reducers: {
      updateTitle (state, payload) {
        return {
          title: payload.title
        }
      }
    }
  },

  render (props) {
    return html`
      <div>
        <input
          value=${props.title}
          oninput=${(e) => {
            props.somethingHappened(e.target.value)
            props.updateTitle({
              title: e.target.value
            })
          }}
        >
        ${props.title}
      </div>
    `
  }
})

app.model(titleComponent.model)

const mainView = (state, prev, send) => {
  const title = titleComponent.component(state, prev, send)

  return html`
    <div>
      <h2>
        Title component demo
      </h2>
      ${title('one', {
        props: {
          somethingHappened (title) {
            console.log('Title one updated', title)
          }
        }
      })}
      <br />
      ${title('two', {
        props: {
          somethingHappened (title) {
            console.log('Title two updated', title)
          }
        },
        initialState: {
          title: 'Easy!'
        }
      })}
    </div>
  `
}

app.router({ default: '/' }, [
  ['/', mainView]
])

document.body.appendChild(app.start())

Running the Examples

  • npm install / yarn install
  • npm run dev
  • visit http://localhost:8888/