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

@js-factory/hoc

v0.3.2

Published

Higher order component

Downloads

2

Readme

hoc

hoc provides a simple APIs to create reusable javascript components. It provides an abstraction from the underlying library use to manage these components. The abstraction enables teams to reuse the same components with different libraries with minimum code changes.

Installation

npm i -S @js-factory/hoc

Motivation

Modern frontend applications are getting complex every day. It has to manage core business logic, complex layouts, and data. It becomes necessary that frontend application architecture follows core software design principles.

In computer science, separation of concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern.

hoc prefers functional programming and allows programmers to write small functions. These functions follow single responsibility principle and do just one thing and produce predictable output.

Dependency

You need to install preact in the host application.

npm i -S preact

Getting Started

// ExampleComponent.js

import { Component } from '@js-factory/hoc';
import componentDidMount from './hooks/afterRender';
import componentShouldUpdate from './hooks/afterUpdate';
import componentWillMount from './hooks/beforeRender';
import componentWillUpdate from './hooks/beforeUpdate';
import componentWillUnmount from './hooks/beforeUnmount';
import onClickHandler from './handlers/onClickHanlder';
import onScrollHandler from './handlers/onScrollHanlder';
import ExampleComponentTmpl from './templates/ExampleComponentTmpl';

const state = {
   salutation: 'Dear'
};

const instanceProps = {
   counter: 0
};

@Component({
   state,
   instanceProp,
   componentDidMount,
   componentWillMount,
   componentWillUpdate,
   componentWillUnmount,
   componentShouldUpdate,
   onClickHanlder,
   onScrollHanlder,
   template: ExampleComponentTmpl
})
export default class ExampleComponent {}
// ExampleComponentTmpl.js
// This is preact functional component

import { h } from 'preact';

const ExampleComponentTmpl = (props) => {
   const { state, instanceProp, onClickHander } = props;
   const { salutation } = state;
   return(
       <div>
           <p> {salutation} user! </p>
           <button onClick={onClickHander}>Say Hello </button>
       </div>
   );
}

Overview

HOC offers four major components e.g. state, template, instanceProps, methods.

state

The state is a plain JavaScript object that represents your component local state. Please read about React state if you are not aware of what a component state is all about. hoc exposes it's own setState to update component state.

const state = {
   ...
   count: 0,
   ...
};

const increment = ({ state, setState }) => {
   const { count } = state;
   return setState({
       count: count + 1
   });
};

instanceProps

The instanceProps are the same as state, unlike the state, any update to instanceProps will not trigger component re-rendering. You can update instanceProps using setInstanceProps method.

const instanceProps = {
   ...
   count: 0,
   ...
};

const increment = ({ instanceProps, setInstanceProps }) => {
   const { count } = instanceProps;
   return setInstanceProps({
       count: count + 1
   });
};

template

A template is functional component represents presentation layer.

Pure JavaScript Functions

You can add as many functions as you need to manage your component state and handle user interactions. These methods could be lifecycle hooks of underlying framework like componentDidMount, componentWillUpdate or simple event handlers.

life cycle hooks

hoc allows developers to use underlying library hooks like componentDidMount, componentWillMount etc. Please refer above component definition.

Note: Unlike react or preact you will not have access to this. hoc will inject all component properties and methods in run time.

event handlers

You can define any dom event handler and bind it to the component. Event handlers are plain javascript functions and surely not tightly coupled with any underlying library.

const onClickHandler = (props, e) => {
 e.preventDefault();
 const { state, setState, getInstanceProp } = props;
 return setState({
   updateMsg: 'I am updated'
 });
}

props contains component state, methods etc. e is a dom event instance.

Other methods

Usually, any component has a lot of helper methods. You can define these methods as part of a component definition.

function foo(props, ...args){
   // function body goes here
}

props contains component state, methods etc. args are runtime arguments supplied