Skip to content

luania/combine-dva-models

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

combine-dva-models

combine dva models

Usage:

combineModels

import { combineModels } from "combine-dva-models";

const modelLoading = {
  state: {
    loading: false
  },
  reducers: {
    loadingChange: (state, { loading }) => ({
      ...state,
      loading
    })
  }
};
const modelSubmitting = {
  state: {
    submitting: false
  },
  reducers: {
    submittingChange: (state, { submitting }) => ({
      ...state,
      submitting
    })
  }
};

const fooModel = combineModels(
  modelLoading,
  modelSubmitting,
  { namespace: "foo" } // 指定一下namespace
);

is equivalent to

const fooModel {
  namespace: "foo",
  state: {
    loading: false,
    submitting: false
  },
  reducers: {
    loadingChange: (state, { loading }) => ({
      ...state,
      loading
    }),
    submittingChange: (state, { submitting }) => ({
      ...state,
      submitting
    })
  }
};

modelSingle(name, initValue)

import { modelSingle } from "combine-dva-models";
const countModel = modelSingle("count", 0);

is equivalent to

const initCount = 0;
const countModel = {
  state: {
    count: initCount
  },
  reducers: {
    countChange: (state, action) => ({
      ...state,
      count: action.count
    }),
    countReset: state => ({
      ...state,
      count: initCount
    })
  }
};

dispatchesSingle(namespace, name)

import { dispatchesSingle } from "combine-dva-models";
const dispaches = dispatchesSingle("foo", "count");

is equivalent to

const dispaches = {
  countChange: value => ({
    type: "foo/countChange",
    count: value
  }),
  countReset: value => ({
    type: "foo/countReset"
  })
};

modelSwitch(name, initValue = false)

import { modelSwitch } from "combine-dva-models";
const loadingModel = modelSwitch("loading");

is equivalent to

const countModel = {
  state: {
    loading: 0
  },
  reducers: {
    loadingOn: (state, action) => ({
      ...state,
      loading: true
    }),
    loadingOff: (state, action) => ({
      ...state,
      loading: false
    })
  }
};

dispatchesSwitch(namespace, name)

import { dispatchesSwitch } from "combine-dva-models";
const dispaches = dispatchesSwitch("foo", "loading");

is equivalent to

const dispaches = {
  loadingOn: () => ({
    type: "foo/loadingOn"
  }),
  loadingOff: () => ({
    type: "foo/loadingOff"
  })
};

About

combine dva models

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published