All Projects → SugarTurboS → rc-redux-model

SugarTurboS / rc-redux-model

Licence: MIT license
一种较为舒适的数据状态管理书写方式,内部自动生成 action, 只需记住一个 action,可以修改任意的 state 值,方便简洁,释放你的 CV 键~

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rc-redux-model

React Social Network
Simple React Social Network
Stars: ✭ 409 (+752.08%)
Mutual labels:  redux-saga, redux-thunk
Redux Subspace
Build decoupled, componentized Redux apps with a single global store
Stars: ✭ 319 (+564.58%)
Mutual labels:  redux-saga, redux-thunk
Fakeflix
Not the usual clone that you can find on the web.
Stars: ✭ 4,429 (+9127.08%)
Mutual labels:  redux-saga, redux-thunk
react-workshops
Online react workshops
Stars: ✭ 36 (-25%)
Mutual labels:  redux-saga, redux-thunk
React Interview Questions
300+ React Interview Questions
Stars: ✭ 151 (+214.58%)
Mutual labels:  redux-saga, redux-thunk
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (+185.42%)
Mutual labels:  redux-saga, redux-thunk
OneArtical
learning and practice redux,react-redux,redux-saga,redux-persist,redux-thunk and so on
Stars: ✭ 61 (+27.08%)
Mutual labels:  redux-saga, redux-thunk
Kea
Production Ready State Management for React
Stars: ✭ 1,805 (+3660.42%)
Mutual labels:  redux-saga, redux-thunk
Redux Saga Thunk
Dispatching an action handled by redux-saga returns promise
Stars: ✭ 212 (+341.67%)
Mutual labels:  redux-saga, redux-thunk
data-flow
frontend data flow explored in React
Stars: ✭ 19 (-60.42%)
Mutual labels:  redux-saga, redux-thunk
react-redux-dashboard
React Redux Dashboard with redux-saga and local-storage support
Stars: ✭ 48 (+0%)
Mutual labels:  redux-saga
crassa
Create React App Server Side Application
Stars: ✭ 16 (-66.67%)
Mutual labels:  redux-saga
twitter-spring-reactjs
🐦 Twitter Clone. Using Java, Spring Boot, PostgreSQL, S3 bucket, JWT, TypeScript, React.js, Redux-Saga, Material-UI
Stars: ✭ 47 (-2.08%)
Mutual labels:  redux-saga
coincharts
Cryptocurrency Price Chart (GDAX)
Stars: ✭ 75 (+56.25%)
Mutual labels:  redux-saga
dva-vue
🌱 Vue and dva-core based
Stars: ✭ 34 (-29.17%)
Mutual labels:  redux-saga
mickey
🤠 Lightweight front-end framework for creating React and Redux based app painlessly.
Stars: ✭ 26 (-45.83%)
Mutual labels:  redux-saga
delivery-app-mobile
🍕React Native food delivery app
Stars: ✭ 143 (+197.92%)
Mutual labels:  redux-saga
fhir-app-starter
🔥 Open Source FHIR App project starter. Start building your app right away.
Stars: ✭ 21 (-56.25%)
Mutual labels:  redux-saga
nextjs-redux-instagram
🌏 The simple Instagram was written by next.js & redux-saga & recompose
Stars: ✭ 48 (+0%)
Mutual labels:  redux-saga
square-mock
A simple mock-up of the Square Dashboard UI Kit using React, Redux, Webpack, Babel, and Firebase.
Stars: ✭ 21 (-56.25%)
Mutual labels:  redux-thunk

rc-redux-model 👋

English | 中文文档

Refer to dva's data flow solution and redux-thunk, internally implement middleware; provide default behavior action, call this action can directly modify any value in state, development is more convenient and concise, support Immutable ~

install

npm install rc-redux-model --save-dev

feature

  • Lightweight and concise, writing data management is as comfortable as writing dva
  • Refer to redux-thunk, implement your own middleware internally to handle asynchronous actions
  • Abandon redux-saga, the asynchronous request can be processed by the user, or the provided method can be called to send, the return is a Promise
  • Provide the default action Action, call this Action, you can directly modify any value in the state
  • Support Immutable, just config it to make your data immutable

how did it come from ?

🚀 usage

Before using, please read this description again, and then read the complete example to get started quickly . 👉 If you want to know how it came, you can check here

  1. Create a new model folder, add a new userModel.js under the folder
import adapter from '@common/adapter';

const userModel = {
  namespace: 'userModel',
  openSeamlessImmutable: true,
  state: {
    classId: '',
    studentList: [],
    userInfo: {
      name: 'PDK',
    },
  },
  action: {
    // demo: dispatch an asynchronous request, change `globalModel` loading status
    // after the asynchronous request is over, modify reducers
    fetchUserInfo: async ({ dispatch, call }) => {
      // before the asynchronous request
      dispatch({
        type: 'globalModel/changeLoadingStatus',
        payload: true,
      });
      let res = await call(adapter.callAPI, params);
      // after the asynchronous request
      if (res.code === 0) {
        dispatch({
          type: 'userModel/setStore',
          payload: {
            key: 'userInfo',
            values: res.data,
          },
        });
        dispatch({
          type: 'globalModel/changeLoadingStatus',
          payload: false,
        });
      }
      return res;
    },
  },
};

export default userModel;
  1. Gather all models, please note that what is exported here is an array
// model/index.js
import userModel from './userModel';

export default [userModel];
  1. Process models, register middleware
// createStore.js
import { createStore, applyMiddleware, combineReducers } from 'redux';
import models from './models';
import RcReduxModel from 'rc-redux-model';

const reduxModel = new RcReduxModel(models);

const reducerList = combineReducers(reduxModel.reducers);
return createStore(reducerList, applyMiddleware(reduxModel.thunk));
  1. Use in the page

Please note that the actions here are all asynchronous actions. Refer to redux-thunk for the implementation of internal middleware. That is to say, one of our dispatch and one action is a corresponding method. Look at the code:

class MyComponents extends React.PureComponent {
  componentDidMount() {
    // demo1 : dispatch an asynchronous action and modify the value of reducers after the request is completed
    // The request is written by yourself in model.action, which supports Promise.
    // Before we need to callback the data after the request, now you can get it directly by Promise.then()
    this.props
      .dispatch({
        type: 'userModel/fetchUserInfo',
      })
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });

    // demo2: call the `automatically generated default action` and directly modify the value of state.userInfo (this method is recommended)
    this.props.dispatch({
      type: 'userModel/setStore',
      payload: {
        key: 'userInfo',
        values: {
          name: 'sugarTeam',
        },
      },
    });
    // demo3: call the `automatically generated default action` to modify the state in the form of an array (this method is recommended)
    this.props.dispatch({
      type: 'userModel/setStoreList',
      payload: [
        {
          key: 'userInfo',
          values: {
            name: 'sugarTeam',
          },
        },
        {
          key: 'classId',
          values: 'sugarTurboS-666',
        },
      ],
    });
  }
}

hooks ?

// Usage with React Redux: Typing the useSelector hook & Typing the useDispatch hook
// https://redux.js.org/recipes/usage-with-typescript#usage-with-react-redux
import { useDispatch } from 'react-redux';

export function useFetchUserInfo() {
  const dispatch = useDispatch();
  return async (userId: string) => {
    // Here I choose to handle the asynchronous by myself.
    // After the asynchronous request is completed, I will pass the data to the reducer.
    const res = await MyAdapterAPI(userId);
    if (res.code === 200) {
      dispatch({
        type: 'userModel/setStore',
        payload: {
          key: 'userInfo',
          values: res.data,
        },
      });
    }
  };
}

Related description

For more information about rc-redux-model, please move to here : rc-redux-model design related instructions

API

Each model receives 5 attributes, as follows

parameter description type defaultValue
namespace the model's namespace, Must, and only string -
state the model's state,Must, and only object {}
action action,not necessary object -
reducers reducer,not necessary object -

default action to change state

  • modify single data
// Class Component  Writing
this.props.dispatch({
  type: '[model.namespace]/setStore',
  payload: {
    key: `${model.state.key}`,
    values: `${your values}`
  }
})

// Hooks Writing
import { useDispatch } from 'react-redux';
const dispatch = useDispatch();
dispatch({
  type: '[model.namespace]/setStore',
  payload: {
    key: `${model.state.key}`,
    values: `${your values}`
  }
})
  • modify multiple data
// Class Component  Writing
this.props.dispatch({
  type: '[model.namespace]/setStoreList',
  payload: [
    {
      key: `${model.state.key}`,
      values: `${your values}`
    },
    {
      key: `${model.state.key}`,
      values: `${your values}`
    }
  ]
})


// Hooks Writing
import { useDispatch } from 'react-redux';
const dispatch = useDispatch();
dispatch({
  type: '[model.namespace]/setStoreList',
  payload: [
    {
      key: `${model.state.key}`,
      values: `${your values}`
    },
    {
      key: `${model.state.key}`,
      values: `${your values}`
    }
  ]
})

Maintainers

@PDKSophia

@SugarTurboS

Contributing

PRs accepted.

License

MIT © 2020 PDKSophia/SugarTurboS


This README was generated with ❤️ by readme-md-generator

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