If you are already a Redux developer, you can compare the Redux versions with Dynadux versions.
Most of the examples are forked Redux examples, and they replaced with Dynadux.
Study all examples to learn the power of Dynadux.
Counter
Todos
Shopping cart
Shopping cart, combine reducers
Counter - Replace Redux with Dynadux
With Dynadux, we don't need the react-redux (and redux, of course).
This example demonstrates how you can replace Redux, but, this is not the approach of Dynadux.
Changes:
- reducers are much more straightforward, decoupled from "dispatch" logic
- index.js is using now the Dynadux store
Counter - Dynadux final version
That is how Dynadux can be used.
Changes:
- the creation of the dynadux store is under the
stores/createCounterStore.js
- the creation of the app store done on the constructor
- also, on the constructor, we bind the
setState
method - we use the
store.state
as a state - the
createCounterStore
exposes business logic methods to update the state
With Dynadux
- The store created in stores/createAppStore.js
- The store provides the state and business logic methods
- In containers, you don't need to dispatch anymore, just use the business methods safely.
- It uses the existed reducers which are simplified
- The actions folder is not needed anymore
- The actions are bound with the reducers in stores/createAppStore.js in a cheap way
- The containers are much cleaner, are react components that make a mapping
- In containers, we can use the new business methods at any time (with Redux, you can't).
- The components remain intact since they are simply viewers
- The containers would be omitted!
A real-world app. Shows how to create 2 sections of the app using the same store.
Shopping cart - Dynadux version
In stores/createAppStore.js
we create our Business Store.
The store is importing only from the reducers
folder. It combines the Sections of the app, the Products, and the Cart.
The createAppStore
function returns an object with two properties. The value of each property taken from the reducer file.
There, each getNNNSectionMethods
function returns an API that used for the needs of each section.
For each Section of the App, the createAppStore
needs
- the initial state
- the actions/reducers
- the exported API
The whole store management is in the reducers. Reducers are the functions that executed per action. So these are the functions that change the state of our store.
This is done at containers/App.js
on the constructor.
The store means the state.
Each section in our app has getters.
To the fetch our Products from the back end we call this.store.products.getAllProducts()
.
To the access and render the Products we call this.store.products.products
, this is a getter.
To access the Total amount of out cart we call this.store.cart.total
, this is a getter.
To check out and send the receipt to the backend, we call this.store.cart.checkout()
.
Now that we have a great API to handle the state of the app, pass it partially to the children's Sections/Components.
Do not pass the whole Store because this will lead to having a monolithic app. Only give what is needed. With Redux, you tend to create monolithic apps since you can dispatch from any Container, any action!.
Cart example with multiple reducers
The created store is different here, as reducers
we pass an array of two dictionaries of action/reducer pairs.
In this way, the ADD_TO_CART
action exists in both Sections, Products, and Cart.
- FAQ Frequently asked questions
- React How to use it in react
- Sections Create sections for applications or big components
- Advanced Dispached promises, boost up your app and more.
- Typescript Tips for Typescript implementations
- Terminology Terminology of dynadux, (is small!)
- History, Undo/Redo Middleware for History, Undo/Redo and Restore Points
- React Dynadux Provider for Dynadux App Stores
- Change Log Changes of Dynadux per semver version
- 🏠 Home, Contents