1.0.23 ā€¢ Published 4 years ago

nedux v1.0.23

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Nedux - The next redux

typescript version size

Why do you waste your time by creating actions/reducers/containers/sagas/... ? \ Just create a store and that's it !

šŸ“¦ Installation

npm install nedux --save

šŸ§² Use Nedux with ...

libraryprovider
Reactreact-nedux
VueJStodo
Angulartodo

šŸ˜ Examples

NameSourceCodesandbox
āœ… Todo Listherehere
šŸ”Ž Logger Middlewareherehere
šŸŽ› Counterherehere

šŸ’» Basic Example

Use it with Typescript ā™„ļø

import { createStore } from 'nedux';

interface Todo {
  id: number;
  text: string;
  completed: boolean;
}

enum Filter {
  ShowAll = 'ShowAll',
  ShowCompleted = 'ShowCompleted',
  ShowActive = 'ShowActive',
}

// Create the store
const todoStore = createStore({
  todos: [] as Todo[],
  filter: Filter.ShowAll,
});

// You can subscribe to field update.
todoStore.subscribe('filter', newFilter => {
  console.log(`filter has changed with ${newFilter}`);
});

// You can get a value.
todoStore.get('filter');
// ā””> 'ShowAll'

// You can override a value.
todoStore.set('filter', Filter.ShowCompleted);

// Or extends value by the previous one.
todoStore.set('todos', todos => [
  ...todos,
  { id: 1, text: 'test', completed: false },
]);

// And that's it !

Or simply with Javascript

import { createStore } from 'nedux';

const todoStore = createStore({
  todos: [],
  filter: 'ShowAll',
});

todoStore.subscribe('filter', newFilter => {
  console.log(`filter has changed with ${newFilter}`);
});

todoStore.get('filter');

todoStore.set('filter', 'ShowCompleted');
todoStore.set('todos', todos => [
  ...todos,
  { id: 1, text: 'test', completed: false },
]);

šŸ“œ Documentation

Import

// ES6
import { createStore } from 'nedux';

// ES5
var createStore = require('nedux').createStore;

createStore(initialState, [middlewares])

Creates a Nedux store with the shape of the initialState.

argumentrequiredtypedescription
initalStateāœ…objectThe intial state of your store.
middlewaresāŒMiddleware[]Middlewares are used to enhance your store see the middleware section to know more.

store

The store object created by createStore it'll allow you to interact with your store.

argumentrequiredtypedescription
keyāœ…stringThe key of the store that you want to get
argumentrequiredtypedescription
keyāœ…stringThe key of the store that you want to override
valueāœ…any or(prevValue: any) => anyThe new value of the key
argumentrequiredtypedescription
keyāœ…stringThe key of the store that you'll subscribe to changes. (give a value of '' will subscribe to all keys changes)
observerāœ…observer or (value: any) => anyAn rxjs observer or a simple callback which will be fired when the store has been updated for the given key

āš“ļø Middlewares

Middleware is the suggested way to extend Nedux with custom functionality. The created store is provided to each middleware. It's easy to subscribe/get/set value to the store inside your middleware. The key feature of middleware is that it is composable. Multiple middleware can be combined together, where each middleware requires no knowledge of what comes before or after it in the chain.

MiddlewareDescription
šŸ”’ nedux-persistAllow you to persist your nedux store

Basic Logger Middleware

import { createStore } from 'nedux';

const loggerMiddleware = store =>
  // we subscribe to all modifications
  store.subscribe('', value => console.log(value));

const store = createStore(
  {
    a: 0,
    b: 'b',
  },
  [loggerMiddleware],
);

store.set('b', 'a');
store.set('a', 1);
store.set('a', a => a * 2);
store.set('b', 'not b');

šŸ— Advised Structure

It usually a good idea to keep the store as small as possible. You can manage your application by structure it as services. Each service will have its own store (if it's needed)

my-service
ā”œā”€ā”€ components # Your components.
ā”‚Ā Ā  ā”œā”€ā”€ AddTodo.tsx
ā”‚Ā Ā  ā”œā”€ā”€ App.tsx
ā”‚Ā Ā  ā”œā”€ā”€ FilterLink.tsx
ā”‚Ā Ā  ā”œā”€ā”€ Footer.tsx
ā”‚Ā Ā  ā”œā”€ā”€ Link.tsx
ā”‚Ā Ā  ā”œā”€ā”€ Todo.tsx
ā”‚Ā Ā  ā””ā”€ā”€ TodoList.tsx
ā”œā”€ā”€ controler.ts # Where you wrap your business logic (link between api/store/ui)
ā”œā”€ā”€ index.tsx # Where you export elements to other services.
ā”œā”€ā”€ store.ts # Where the store is created with the initial state.
ā””ā”€ā”€ types.ts # Where you put your service types.

šŸš€ Why choose Nedux over Redux ?

  • No more actions
  • No more dispatch
  • No more reducers
  • No more provider
  • Fully functionnal usage
  • Easiest to understand
  • No "magical" effect (all is traceable)
  • No need to use external tools to debug (again all is traceable)
  • Easiest to learn
  • Fully typed (if you're coding in typescript you will ā™„ļø it !)
  • Less code to write
  • Faster and lighter (no react context, no HOC)

You just write less to do the same.

šŸ„Š Redux todos VS Nedux todos (same code)

Feel free to inspect the structure of both of them (Redux and Nedux) and how Nedux is implemented.

ReduxNeduxDiff (less is better)
number of files1311-15.4%
number of lines224174-22.3%
number of characters43433298-24.0%
time for first render~10.5 ms~8.5 ms-23.5%
add todo~0.8 ms~0.6 ms-33.3%

šŸ„Š Redux Counter VS Nedux Counter (same code)

Again feel free to test it yourself here.

Render timeReduxNeduxDiff (less is better)
with 9999 items0.743s0.481s-35.3%

šŸ— Structure

# Redux Todos
ā”œā”€ā”€ actions
ā”‚Ā Ā  ā””ā”€ā”€ index.js
ā”œā”€ā”€ components
ā”‚Ā Ā  ā”œā”€ā”€ App.js
ā”‚Ā Ā  ā”œā”€ā”€ Footer.js
ā”‚Ā Ā  ā”œā”€ā”€ Link.js
ā”‚Ā Ā  ā”œā”€ā”€ Todo.js
ā”‚Ā Ā  ā””ā”€ā”€ TodoList.js
ā”œā”€ā”€ containers
ā”‚Ā Ā  ā”œā”€ā”€ AddTodo.js
ā”‚Ā Ā  ā”œā”€ā”€ FilterLink.js
ā”‚Ā Ā  ā””ā”€ā”€ VisibleTodoList.js
ā”œā”€ā”€ index.js
ā””ā”€ā”€ reducers
    ā”œā”€ā”€ index.js
    ā”œā”€ā”€ todos.js
    ā””ā”€ā”€ visibilityFilter.js
# Nedux Todos
ā”œā”€ā”€ components
ā”‚Ā Ā  ā”œā”€ā”€ AddTodo.tsx
ā”‚Ā Ā  ā”œā”€ā”€ App.tsx
ā”‚Ā Ā  ā”œā”€ā”€ FilterLink.tsx
ā”‚Ā Ā  ā”œā”€ā”€ Footer.tsx
ā”‚Ā Ā  ā”œā”€ā”€ Link.tsx
ā”‚Ā Ā  ā”œā”€ā”€ Todo.tsx
ā”‚Ā Ā  ā””ā”€ā”€ TodoList.tsx
ā”œā”€ā”€ controler.ts
ā”œā”€ā”€ index.tsx
ā”œā”€ā”€ store.ts
ā””ā”€ā”€ types.ts

šŸ§© Scripts used

# Compute number of files
find $SRC_FOLDER -type f | wc -l

# Compute number of lines
find $SRC_FOLDER -type f -exec cat {} \; | grep -v -e '^$' | grep -v -e '^//' | wc -l

# Compute number of characters
find $SRC_FOLDER -type f -exec cat {} \; | grep -v -e '^$' | grep -v -e '^//' | tr -d '[:space:] ' | wc -c

šŸ”Ž Profiling method

Profiling is made with React Profiling following this configuration :

NavigatorChrome 78.0.3904.108 (64-bit)
Profiling SoftwareReact Developer Tools 4.2.1
OSMacOS Catalina 10.15.1
ModelMacBook Pro (15-inch, 2018)
Processor2.2 GHz 6-Core Intel Core i7
Memory16 GB 2400 MHz DDR4
GraphicIntel UHD Graphics 630 1536 MB

šŸ“‹ Todos

  • Add sandbox for each examples
  • Add tests
  • Be more accurate on performance comparison
  • Add more examples
  • Type cleaning
  • Add CI
  • Add VueJS connector
  • Add Angular connector

šŸ™‹šŸ¼ Contributions

All Pull Requests, Issues and Discussions are welcomed !

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago