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

reduxlare

v0.1.2

Published

declarative redux

Downloads

10

Readme

Reduxlare

Reduxlare is a declarative library for redux. It allows you to create your reducers, action creators and selectors declaratively. The nicest thing about redux is using it after it's already set up. Setting it up initially can be a pain and refactoring can also be difficult. Reduxlare makes the initial setup and refactoring simple. The main idea behind Reduxlare is that every field in your state has properties that you use to modify the state. Reduxlare isn't just Redux without the boilerplate, it's a different experience.

Usage

The main point of entry for reduxlare is the createSlice() method. You use createSlice() to create your reducer, selectors and dispatchers for a slice of your state. You then add your reducer to the combineReducers() method provided by redux.

// slice.js

import { createSlice, incrementable } from 'reduxlare';

/*
* createSlice takes two arguments. The name of the slice, and a list of fields
* Each field contains a key, an initialState, and a list of properties.
* Properties are used to generate dispatchers for the field.
*/
const { reducer, dispatchers, selectors } = createSlice('someSlice', [
  { key: 'counter', initialState: 0, properties: [incrementable] },
]);

export { reducer, dispatchers, selectors };
// store.js

import { createStore, combineReducers } from 'redux';
import { reducer } from './slice';

const store = createStore(combineReducers({ someSlice: reducer }));
// SomeComponent.jsx

import { connect } from 'react-redux';
import { combine } from 'reduxlare';
import { dispatchers, selectors } from './slice';

const SomeComponent = ({ counter, incrementCounter, decrementCounter }) => {
  <div>
    <p>{counter}</p>
    <button onClick={incrementCounter}>increment</button>
    <button onClick={decrementCounter}>increment</button>
  </div>;
};

/*
* Use the combine method provided by reduxlare if you want to use
* more than one selector or dispatcher in your component
*/
export default connect(
  selectors.counter,
  combine(
    dispatchers.counter.increment,
    dispatchers.counter.decrement
  )
)(SomeComponent);

Nested Fields

const { reducer, dispatchers, selectors } = createSlice('someSlice', [
  {
    key: 'parent',
    properties: [settable]
    fields: [{ key: 'child', initialValue: 'hey', properties: [settable] }],
  },
]);

// set parent value
dispatchers.parent.set

// set child value
dispatchers.parent.child.set

// get parent value
selectors.parent

//get child value
selectors.parent.child

Properties

Each field has a list of properties. Properties describe how a field can be changed. Each property provides a method to your field in dispatchers returned by createSlice(). Reduxlare provides several properties for you to use in your application. You can also define your own properties by using the Property class provided by Reduxlare, though you hopefully shouldn't have to do that too often.

List of Properties

Settable

import { settable } from 'reduxlare'

dispatchers.key.set

props.setKey(value)

The setKey function passed into a components props takes a parameter called value. setKey will set the key in your redux store to the value parameter provided.

Toggleable

toggleable is used for boolean values to toggle between true and false

import { toggleable } from 'reduxlare'

dispatchers.key.toggle

props.toggleKey()

ListProperties

listProperties bundles together the poppable, pushable, and settableAtIndex properties. You can also use the individual properties by themselves for more fine grained control.

import { listProperties } from 'reduxlare'

dispatchers.key.pop

dispatchers.key.push

dispatchers.key.setAtIndex

props.popKey()

Removes the element at the last index of the list.

props.pushKey(value)

Pushes value to the end of the list.

props.setAtIndexKey(value, index)

Sets the element at index in the list to value. Throws an error if there is no element at index.

AsyncProperty

Coming soon.

Creating your own Properties

import { Property } from 'reduxlare';

const customProperty = new Property(actionType, reducer, actionCreator, prefix);

export default customProperty;

actionType is the action type that will be dispatched from the corresponding dispatcher function generated when a field has the custom property.

reducer is the reducer used for actionType. Note that you do not have to do the usual check that action.type === actionType as you do in vanilla redux.

actionCreator is a function that returns an action. It directly corresponds to the dispatch function passed in as a prop when you connect your component. Note that unlike action creators in vanilla redux, actionCreator does not need you to specify an action type or a fields key.

prefix is the prefix for the dispatch function passed in as a prop, as well as the name of the function in the dispatchers return from createSlice(). For example, settable's prefix is 'set' and we have dispatchers.key.set, and props.setKey().