All Projects β†’ tvler β†’ Compose State

tvler / Compose State

Licence: mit
Compose multiple setState or getDerivedStateFromProps updaters in React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Compose State

Masala Parser
Javascript Generalized Parser Combinators
Stars: ✭ 110 (-7.56%)
Mutual labels:  functional-programming
Ramda Debug
🐏 Debugging for Ramda.
Stars: ✭ 113 (-5.04%)
Mutual labels:  functional-programming
Articles
thoughts on programming
Stars: ✭ 1,515 (+1173.11%)
Mutual labels:  functional-programming
Lift
constexpr C++17 library for simplifying higher order functions in application code
Stars: ✭ 111 (-6.72%)
Mutual labels:  functional-programming
F
Functional stuff for Python
Stars: ✭ 113 (-5.04%)
Mutual labels:  functional-programming
Kind
A modern proof language
Stars: ✭ 2,075 (+1643.7%)
Mutual labels:  functional-programming
React Collection Helpers
A suite of composable utility components to manipulate collections.
Stars: ✭ 109 (-8.4%)
Mutual labels:  functional-programming
Awesome Functional Python
A curated list of awesome things related to functional programming in Python.
Stars: ✭ 1,637 (+1275.63%)
Mutual labels:  functional-programming
Rxtuples
Simple tuples to use with RxJava [STABLE]
Stars: ✭ 113 (-5.04%)
Mutual labels:  functional-programming
Dunai
Classic and Arrowized Functional Reactive Programming, Reactive Programming, and Stream programming, all via Monadic Stream Functions
Stars: ✭ 115 (-3.36%)
Mutual labels:  functional-programming
Freasy Monad
Easy way to create Free Monad using Scala macros with first-class Intellij support.
Stars: ✭ 112 (-5.88%)
Mutual labels:  functional-programming
Wonder Editor
Functional 3D Webgl Editor
Stars: ✭ 113 (-5.04%)
Mutual labels:  functional-programming
Cilib
Typesafe, purely functional Computational Intelligence
Stars: ✭ 114 (-4.2%)
Mutual labels:  functional-programming
Scalajs React
Facebook's React on Scala.JS
Stars: ✭ 1,524 (+1180.67%)
Mutual labels:  functional-programming
Pointless
Pointless: a scripting language for learning and fun
Stars: ✭ 116 (-2.52%)
Mutual labels:  functional-programming
Purescript Spec
Testing framework for Purescript
Stars: ✭ 108 (-9.24%)
Mutual labels:  functional-programming
Combinators Js
🐦 Some combinators
Stars: ✭ 114 (-4.2%)
Mutual labels:  functional-programming
Aardvark.base
Aardvark is an open-source platform for visual computing, real-time graphics and visualization. This repository is the basis for most platform libraries and provides basic functionality such as data-structures, math and much more.
Stars: ✭ 117 (-1.68%)
Mutual labels:  functional-programming
Cyclejs
A functional and reactive JavaScript framework for predictable code
Stars: ✭ 9,996 (+8300%)
Mutual labels:  functional-programming
Functional Way
Write small programs (eg -algorithms) in a functional way.
Stars: ✭ 115 (-3.36%)
Mutual labels:  functional-programming

✍️ compose-state

compose-state is a library that composes multiple state updaters in React.

example

compose-state works with the standard setState parameters – objects or functions – so you don’t have to learn any new syntax. It’s also compatible with React’s new getDerivedStateFromProps lifecycle method.

Use

Install

yarn add compose-state or npm install compose-state

Import

import composeState from 'compose-state';

// or

import {
  composeState,
  composeDerivedStateFromProps,
} from 'compose-state';

API

composeState([updaters])

Returns an updater that can be used with setState. Calling this produces the same result as calling or accessing each given updater from right to left, merging each partial state. If a given updater is a function, its prevState value is the previous state merged with the current partial state.

Arguments
Name Type Description
[updaters] (...Updater) Functions that can be used with setState, or partial state objects

composeDerivedStateFromProps([updaters])

Returns an updater that can be set as a component's getDerivedStateFromProps static value. Calling this produces the same result as calling or accessing each given updater from right to left, merging each partial state. If a given updater is a function, its prevState value is the previous state merged with the current partial state.

Arguments
Name Type Description
[updaters] (...Updater) Functions that can be set as a component's getDerivedStateFromProps static value, or partial state objects

Benefits

Simplify your updaters and React code

Let's say you want to call setState and do two things

  1. Increment a score value by 1
  2. Log the current time to an array

Both of these updaters need to be functional, since they rely on the previous state for their return values.

const updateScore = s => ({ score: s.score + 1 });
const logTime = s => ({ log: [...s.log, Date.now()] });

Normally, we would need to call setState for both of these functions

class Game extends Component {
  onScore = () => {
    this.setState(updateScore);
    this.setState(logTime);
  };
  // ...
}

...or we rewrite the two updaters into one larger function.

const updateScoreLogTime = s => ({
  score: s.score + 1,
  log: [...s.log, Date.now()],
});

But with compose-state, we can keep these two updaters independent, and we won't have to bulk up our component code with more setState calls.

const updateScoreLogTime = composeState(updateScore, logTime);

class Game extends Component {
  onScore = () => {
    this.setState(updateScoreLogTime);
  };
  // ...
}

compose-state isn't dependent on React at all, it's just a big reducer function. This allows you to build and compose as many updaters as you want while keeping your actual component code simple and maintainable.

Easily mix functional and object updaters

compose-state accepts both objects and functions, just like setState. This allows you to mix static and dynamic updaters without increasing the complexity of any individual parameter.

const defaultValue = { value: 0 };
const incrementOther = s => ({ other: s.other + 1 });

this.setState(
  composeState(defaultValue, incrementOther)
);

Compatibility with getDerivedStateFromProps

compose-state comes with a composeDerivedStateFromProps function to use with React's new getDerivedStateFromProps lifecycle method.

const updater1 = (nextProps, prevState) => {
  // ...
}
const updater2 = (nextProps, prevState) => {
  // ...
}

class App extends Component {
  static getDerivedStateFromProps = composeDerivedStateFromProps(
    updater1, updater2
  )
  // ...
}

It's just normal, boring React

While more formal state managers push developers away from controlling state in React, compose-state simply enhances state control methods that are primitive to the platform.

compose-state is a lot like Classnames. It's a helper function that makes setState calls more declarative and easier to construct, just like how Classnames is a helper function that makes className values more declarative and easier to construct.

Further reading

Functional setState is the future of React by Justice Mba

Best kept React secret: you can declare state changes separately from the component classes. by Dan Abramov

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].