From Redux to Rematch
Moving from Redux to Rematch involves very few steps.
1. Setup Rematch init with Redux​
Imagine a simple app than increments a value in a redux store written in React.
tsximportReact from "react";importReactDOM from "react-dom";import {init } from "@rematch/core";import {Provider } from "react-redux";importthunk from "redux-thunk";Âimportsharks from "./redux/sharks";importApp from "./App";Â// generate Redux storeconststore =init ({redux : {reducers : {sharks ,},middlewares : [thunk ],},});ÂconstRoot = () => (<Provider store ={store }><App /></Provider >);ÂReactDOM .render (<Root />,document .querySelector ("#root"));
tsximportReact from "react";importReactDOM from "react-dom";import {init } from "@rematch/core";import {Provider } from "react-redux";importthunk from "redux-thunk";Âimportsharks from "./redux/sharks";importApp from "./App";Â// generate Redux storeconststore =init ({redux : {reducers : {sharks ,},middlewares : [thunk ],},});ÂconstRoot = () => (<Provider store ={store }><App /></Provider >);ÂReactDOM .render (<Root />,document .querySelector ("#root"));
2. Mix reducers & models​
Our Redux reducers are currently like this:
tsconstINCREMENT = "sharks/increment";Âexport constincrementSharks = (payload : number) => ({type :INCREMENT ,payload ,});Âexport default (state = 0,action : {payload : number;type : string }) => {switch (action .type ) {caseINCREMENT :returnstate +action .payload ;default:returnstate ;}};
tsconstINCREMENT = "sharks/increment";Âexport constincrementSharks = (payload : number) => ({type :INCREMENT ,payload ,});Âexport default (state = 0,action : {payload : number;type : string }) => {switch (action .type ) {caseINCREMENT :returnstate +action .payload ;default:returnstate ;}};
But now we'll move them to Rematch Models, create a new file called /models/sharks.js:
tsexport default {state : 0,reducers : {increment : (state ,payload : number) =>state +payload ,},};
tsexport default {state : 0,reducers : {increment : (state ,payload : number) =>state +payload ,},};
info
Both snippets (Redux one and Rematch Model) are equivalent.
Now, add it to your init() method and remove redux-thunk because isn't required with Rematch:
tsimport {init } from "@rematch/core";Âconststore =init ({models : {sharks ,},});
tsimport {init } from "@rematch/core";Âconststore =init ({models : {sharks ,},});
Views probably will work out of the box, because we're compatible with react-redux.
Enjoy your refactored code-base :)