1.0.3 • Published 5 years ago

combine-dva-models v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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"
  })
};
1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago