Skip to content

v3.0.0

Compare
Choose a tag to compare
@ctrlplusb ctrlplusb released this 23 Jul 15:53

v3 is considered the realisation of the "final" Easy Peasy API - taking all the evolution and learning from v2 to produce a long term stable API that we will commit to supporting and will do our best to avoid breaking changes moving forward.

New Features

Hot reloading support

Hot reloading is supported via the store.reconfigure(model) API. See #168

New actionOn and thunkOn APIs

These are the new and only APIs by which to define action/thunk listeners with.

The v3 website has been updated with tutorials and API docs introducing these APIs.

We are really sorry about the churn around listener APIs! This API was driven by community feedback so feeling far better about it. πŸ‘

Breaking Changes

Removed deprecated APIs

Thunks can be either asynchronous and synchronous

Using async/await or returning a Promise from a thunk will maintain its previous async behaviour.

However, if you do neither of the above your thunk will be executed synchronously. Therefore you can now get eager updates to your state if all you do is dispatch actions within your thunk. This can be handy for encapsulating logic based action dispatching.

For example

addProduct: thunk((actions, payload) => {
  switch (payload.type) {
    case 'SHOES': 
		actions.addShoe(payload);
        break;
    case 'VEGETABLE':
        // ...
  }
});

Returning immutable state from actions

If you prefer to return new immutable state from your actions, rather than mutating the state, you need to set the new disableImmer flag.

import { createStore, action } from 'easy-peasy';

const model = {
  todos: [],
  addTodo: action((state, payload) => {
    // πŸ‘‡ new immutable state returned
    return [...state, payload];
  })
}

const store = createStore(model, {
  disableImmer: true // πŸ‘ˆ set the flag
})

Failing to disable immer may result in strange errors if you are using computed properties.

computed

In order to optimise the Typescript experience we have made a fairly small change to the computed API. If you wish to use state resolvers, these now need to be defined as the first argument.

Before

const basketModel = {
  productIds: [],
  products: computed(
    (productIds, products) => productIds.map(id => products[id]),
    [
      state => state.productIds,
      (state, storeState) => storeState.products.items
    ]
  )
};

After

const basketModel = {
  productIds: [],
  products: computed(
    [
      state => state.productIds,
      (state, storeState) => storeState.products.items
    ],
    (productIds, products) => productIds.map(id => products[id])
  )
};

Computed properties not using state resolvers remain unchanged.

useStoreState API update

useStoreState(previouslyuseStore) no longer needs/accepts the dependencies 2nd argument. Your state will get mapped correctly if they use external values, like props, within the mapState` function.

Typescript

We officially support >= [email protected]. Although we recommend using the latest version ([email protected] at the time of writing), in order to ensure you are up to date with the latest bug fixes.

Create React App users can just install [email protected] as a dev dependency and the CRA build system will use that version. You may get a warning printed to your console, however, we experienced no issues with this. πŸ‘

Note: using a lower version of TypeScript 3.x may still work, however, you may have issues.

Hooks

You will have noted above that the useStoreState, useStoreActions, and useStoreDispatch are no longer attached to the store instance. You need to use the createTypedHooks helper instead.

import { createTypedHooks } from 'easy-peasy';
import { StoreModel } from './model';

const { useStoreActions, useStoreState, useStoreDispatch } = createTypedHooks<StoreModel>();

export default {
  useStoreActions,
  useStoreState,
  useStoreDispatch
}

Computed

The Computed type no longer requires you to define the types for state resolvers. These will automatically be inferred.

Before

interface BasketModel {
  productIds: string[];
  products: Computed<
    BasketModel, 
    Product[], 
    ResolvedState2<string[], Product[]>, 
    StoreModel
   >
}

After

interface BasketModel {
  productIds: string[];
  products: Computed<
    BasketModel, 
    Product[],
    StoreModel
   >
}

Commits

  • Update overview.md: f5fbe87
  • Removes deprecated code: ba955a5
  • Removes unused assets: 0a06ee8
  • Cleans up and organises the typescript definitions: 78dc4d4
  • Adds depth limit to State and Actions typescript definitions: 55375dc
  • Fixes index signatures on state: 7ce3188
  • Dispatch no longer has actions bound to it and fixes to types: ff6cd76
  • Removes unused deps and bumps version: 858c96b
  • Adds yarnrc: 0bdbdad
  • Fixes thunk payloads: 8b8bdce
  • Bumps version: bff2d9c
  • Adds test case for state any: 7545e63
  • Breaking change listenTo actions now resolved via callback funciton: 8086cb2
  • Progress on new tutorial: 70287db
  • Updates docs: 4b30b9f
  • Updates website: 754d6a2
  • Website updates: 85e8f7b
  • Fixes useStoreState so that an update gets handled in same render cycle across hook instances: 6f2826f
  • Bumps version: 56c871f
  • Fixes multi hook render cycle issue: 42dc2e1
  • Bumps version: 09ac40f
  • Removes the need to define dependencies on useStoreState: 4cb3ec3
  • Bumps version: cd14db1
  • Removes instance of useStoreState dependencies: 1ff97f7
  • Updates deps: 6145efa
  • Website updates: 75ce14c
  • Reverts the change that removed bound action creators on the stores dispatch: e8aa7bd
  • Fixes action name helper typings: d37943d
  • Bumps version: de12c2c
  • Fixes store so it does not kill class based properties: 1c51bf7
  • Bumps version: b3ad4eb
  • Updates website: 8c4ffa2
  • Installs v3 of ts-toolbelt and bumps version: d926ce3
  • Bumps version: e300c57
  • Creates a simplified immer produce and fixes debug helper: 461d42e
  • Adds an additional test case for typescript generic interface models: 3f82175
  • Bumps version: 7a382e8
  • Adds new listeners APIs as per #247: #251
  • Updates ts-toolbelt.: d93a01c
  • Moves prop-types from peer deps to deps: ca321b2
  • Updates to Typscript 3.5.3: 39b80a3
  • Removes prop-types from dev deps: 88aed9a
  • Upgrades deps: f77cb49
  • Adds a comment to a typescript test case: 925c68c
  • Adds createTypedHooks API back: 3a16e10
  • Bumps version: 332c80b
  • Updates website: b23993a
  • Updates website: 49e4f25
  • Adds the ability to reconfigure a store: 270dc6a
  • Bumps version: 80112da
  • Updates website: 809c278
  • Updates website: dfc4209
  • Updates website: 68721b8
  • Updates website: 58204ff
  • Updates website: 9f3152b
  • Adds now deploy config: 0eada96
  • Removes the hooks from store instance: 49ee9e3
  • Updates website: 9261d23
  • Bumps version: 9e519d0
  • Updates website: 672e786
  • Adds tests and docs about testing: 06e67ac
  • Adds 100% code coverage: d35153c
  • Code housekeeping: 95eb5ef
  • Updates website: bee0ad6
  • Bumps version: e4859b9
  • Updates website: 0efd3d0
  • Updates readme: 2be472b
  • Refactor listener TS api: #259
  • Type experiment: 17f8215
  • Refactors computed and simplifies the TS defs for it.: 13526ba
  • Updates docs: 9f4f8cc
  • Bumps version: 0646512
  • Enables disabling of immer: ada83d7
  • Code cleanup and refactoring. Listeners correctly prefixed: 292804f
  • Moves listeners into a getListeners API of the store instance: c9ee9d8
  • Updates documentation per new getListeners: 2706d22
  • Fixes disableImmer behaviour: 5ae789a
  • Bumps version: 7c97880
  • Updates release number in docs: 6147021
  • Minor docs update and definition fix: dfaf0cc
  • Updates website: 9f221da
  • Bumps version: 6efd17e
  • Bumps version: 961a4fc
  • Converts thunks so that they can be asynchronous or synchronous: c465d98
  • Upgrades deps: 78923e3
  • Fixes computed property reducer access cases: d9aa182
  • Updates deps: 412c77f
  • V3: a297302