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

redux-component-host

v0.3.0

Published

Compose your redux application from reusable components

Downloads

10

Readme

redux-component-host

Compose your redux application from reusable components.

## Usage

Install:

$ npm install --save redux-component-host

Quick start:

import { AppState } from './state';
import createHistory from 'history/createBrowserHistory';
import ComponentHost from 'redux-component-host';

// instantiate the host and load the components from the components dir
const host = new ComponentHost<AppState>();
host.loadComponents(__dirname + '/components');

// create the history object and render the application
const history = createHistory();
host.render(history);

The loadComponents method looks for files called 'component' or '[something].component', and requires them. Then it looks for a function called initReduxComponent (by default) and invokes it, passing the ComponentHost instance. The function can then register reducers, state, routes, etc.

export function initReduxComponent(host: ComponentHost<AppState>) {
  host
    .addReducer({users: reducer})
    .setInitialState('users', [])
    .addPart('/users', ListUsersComponent);
}

Sections

A "section" is an area on a rendered page which shows one or more of a number of "parts", depending on the current route. The "main" section is the one which is used in render. Parts which are rendered within this section may also render sub-sections, using the getSection method of ComponentHost. This method takes two parameters: the name of the section, and whether the section is to show only the first part matching the current route, rather than all matching.

The example below defines an extra section called "nav", and shows how it can be used to construct a navigation area:

// this dummy interface will constrain the names that are allowed to
// be supplied to `addPart` and `getSection`
interface Sections { main; nav; }
const host = new ComponentHost<AppState, Sections>();

// in the Users component
// add the main component
host.addPart('/users', UsersComponent);
// add a nav link to the "nav" section
// last param means don't match exactly, so will display for all routes
host.addPart('/', UsersNavLink, 'nav', false);

// define a page component, a sort of master page
const Page = (props) => (
  <div>
    <nav>{host.getSection('nav', false)}</nav>
    <div>
      {props.children}
    </div>
  </div>
);

// render the app
host.render(history, Page);

API

ComponentHost<TState, TSections extends DefaultSections = DefaultSections> class

A container for registering Redux components.

Generic arguments

  • TState - the application state type
  • TSections - an interface with keys to name the sections

setInitialState<K extends keyof TState>(key: K, value: TState[K])

Sets the initial value for the specified key of the application state.

Params

  • key - the key of the state hash to set
  • value - the initial value

addReducer(reducer: ReducerFragment<TState>)

Registers a reducer.

Params

  • reducer - the reducer to register

addEffector(effector: SideEffects.Effector)

Registers a side-effect effector.

Params

  • effector - the effector function to register

addEnhancer(enhancer: Redux.StoreEnhancer<TState>)

Registers a Redux store enhancer.

Params

  • enhancer - the enhancer function to register

addMiddleware(middleware: Redux.Middleware)

Registers a Redux store middleware function.

Params

  • middleware - the middleware function to register

addPart(path: string, Component: React.ComponentClass<any> | React.StatelessComponent<any>, section: keyof TSections = 'main', exact=true)

Adds a page part.

Params

  • path - the path the part is to show for
  • Component - the component to render
  • section - the name of the page section the component is to be rendered in
  • exact - whether or not the route matching is to be exact

loadComponents(requireContext, fnName = 'initReduxComponent')

Automagically loads components from using the specified webpack require context. By default it looks for an exported function called initReduxComponent, and runs it with this host as the first argument, and a 'wait-for-dependencies' waiter as the second argument.

Params

  • requireContext - a webpack require.context() result
  • fnName - the name of the function to run

get store()

Gets the store - created on first access.

getSection(name: keyof TSections, exclusive=true)

Gets the section with the specified name.

Params

  • name - the name of the section to get
  • exclusive - true to render only the first part that matches the route; otherwise, false

render(history: History, Page: React.StatelessComponent<any> | React.ComponentClass<any> = DummyComponent, rootId='root')

Render the application.

Params

  • history - the history implementation to use
  • Page - the page component to wrap the whole application in
  • rootId - the ID of the root element to render the application into

Part interface

A page part for a particular route path.

section: string

The name of the section the part will be rendered in.

path: string

The route path the section is to be rendered for.

Component: React.ComponentClass<any> | React.StatelessComponent<any>

The React component representing the section.

exact: boolean

Whether or not to use exact route matching.

ReducerFragment<TState> type

A reducer for the given application state or part thereof.