All Projects → alexfoxy → Juicr.js

alexfoxy / Juicr.js

Licence: mit
A simple (and tiny <1kb) redux inspired reducer for handling state changes.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Juicr.js

RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (-36.27%)
Mutual labels:  flux, state-management, reducer
mini-kotlin
Minimal Flux architecture written in Kotlin.
Stars: ✭ 20 (-80.39%)
Mutual labels:  flux, state-management
Use Substate
🍙 Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (-4.9%)
Mutual labels:  state-management, reducer
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+2764.71%)
Mutual labels:  state-management, reducer
Vuex-Alt
An alternative approach to Vuex helpers for accessing state, getters and actions that doesn't rely on string constants.
Stars: ✭ 15 (-85.29%)
Mutual labels:  flux, state-management
react-stateful-component
Functional stateful React components with sideEffect support
Stars: ✭ 19 (-81.37%)
Mutual labels:  state-management, reducer
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (+166.67%)
Mutual labels:  state-management, reducer
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (-5.88%)
Mutual labels:  flux, state-management
Grox
Grox helps to maintain the state of Java / Android apps.
Stars: ✭ 336 (+229.41%)
Mutual labels:  flux, state-management
Redux Machine
A tiny library (12 lines) for creating state machines in Redux apps
Stars: ✭ 338 (+231.37%)
Mutual labels:  state-management, reducer
Dutier
The immutable, async and hybrid state management solution for Javascript applications.
Stars: ✭ 401 (+293.14%)
Mutual labels:  flux, state-management
Freezer
A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
Stars: ✭ 1,268 (+1143.14%)
Mutual labels:  flux, state-management
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+541.18%)
Mutual labels:  flux, state-management
litestate
An ambitiously tiny, gizp ~800b, flux-like library to manage your state
Stars: ✭ 31 (-69.61%)
Mutual labels:  flux, state-management
useReduction
useReducer without boilerplate
Stars: ✭ 36 (-64.71%)
Mutual labels:  flux, reducer
Loona
🌕 Application State Management done with GraphQL
Stars: ✭ 270 (+164.71%)
Mutual labels:  flux, state-management
react-evoke
Straightforward action-driven state management for straightforward apps built with Suspense
Stars: ✭ 15 (-85.29%)
Mutual labels:  flux, state-management
riduce
Get rid of your reducer boilerplate! Zero hassle state management that's typed, flexible and scalable.
Stars: ✭ 14 (-86.27%)
Mutual labels:  state-management, reducer
Fluorine
[UNMAINTAINED] Reactive state and side effect management for React using a single stream of actions
Stars: ✭ 287 (+181.37%)
Mutual labels:  flux, state-management
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+455.88%)
Mutual labels:  flux, state-management

juicr.js

A simple (and tiny <1kb) redux inspired reducer for handling state changes. Works well with React.js & React Native but can be combined with any front end library, or even vanilla JS template literals.

Why?

I liked the redux pattern but the amount of boiler plate seemed overkill, especially for smaller projects.

Examples

All examples use the same juicr reducer code.

Setup

Add the package to your project either with:

# npm
npm install juicr.js

# yarn
yarn add juicr.js

or for browsers:

<script src="https://cdn.jsdelivr.net/npm/juicr.js" ></script>

Simple example

  1. Create a new Juicr with some initial state:
const juicr = new Juicr({ initialState: { count: 0 } })
  1. Add an action with a name and a function that returns the changed state:
juicr.action("count", (state, amount) => {
	return { count: state.count += amount }
})
  1. Listen to state changes. You can either listen to a single property, an array or use * to listen to all changes:
juicr.listen("*", (changedState, _state) => {
	document.body.innerHTML = changedState.count
	/* or your front end library update function e.g. this.setState({ ...changedState }) */
})
  1. Dispatch actions to the Juicr:
setInterval(() => {
	juicr.dispatch("count", 1)
}, 1000)

Play with this example in CodePen.

For use with React see: Use with React & React Native

API

new Juicr({ initialState={}, dev=false })

Initializes a new Juicr. Pass in an initialState object and an optional dev flag. When dev mode is enabled all changes to the state are printed to the console.

juicr.action('actionName', (data, _state) => { })

Adds a dispatchable action to the Juicr. Specify the actionName and a function that returns the state changes. The data is passed in from the dispatch call as well as the current Juicr _state. For example:

juicr.action('delete', (state, { id }) => {
	return { items: state.items.filter(t => t.id !== id ) }
})

juicr.dispatch('actionName', data)

Dispatches an action with data on your Juicr. For example:

juicr.dispatch("delete", { id: 1 })

juicr.listen('propName', (changedState, _state) => { })

Listens to changes to the state either from an action. You can either specify a single property:

juicr.listen("items", (changedState, _state) => { })

An array of properties:

juicr.listen(["propA", "propB"], (changedState, _state) => {})

Or use the special character * to listen to any changes on the state:

juicr.listen("*", (changedState, _state) => {})

Reactions & juicr.updateState()

Reactions have been removed in version 1.1.0 to simplify code base. If you need computed properties use listen and updateState, e.g.

juicr.listen('count', ({ count }, _state) => {
	juicr.updateState({ countIsPositive: count > 0 })
})

Asynchronous actions

Actions can return a Promise which resolves with the state changes. When dispatching use .then for triggering other actions or .catch for errors, e.g.

juicr.action("setText", (state, text) => {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve({ text })
		}, 100)
	})
})

juicr.dispatch("setLoading", true)
juicr.dispatch("setText", "hello").then((changedState) => {
	juicr.dispatch("setLoading", false)
	// changedState.text === "hello"
})

Multiple Juicrs

Larger projects may benefit from using multiple Juicrs for different parts of your application data. For example you might have one Juicr for the user state and another for a list of todos.

Use with React & React Native

Using juicr.js with React.js & React Native is easy. The simplest approach is to listen to all changes * in your main app component and use setState to update your state:

// App.js
constructor() {
	...
    this.juicr.listen("*", (changedState, _state) => {
      this.setState({ ...changedState })
    })
    ...
}

Then pass the juicr.dispatch function to components:

<MyComponent dispatch={this.juicr.dispatch} />

Alternatively you could pass the entire juicr to your components and let them handle their own internal state and listen for changes, e.g:

// UserHeader.js
constructor(props) {
	...
	this.state = { username: '', photoUrl: '' }

	props.userJuicr.listen(["username", "photoUrl", (changedState, _state) => {
		this.setState({ ...changedState })
	})
	...
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].