combine dva models
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
})
}
};
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
})
}
};
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"
})
};
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
})
}
};
import { dispatchesSwitch } from "combine-dva-models";
const dispaches = dispatchesSwitch("foo", "loading");
is equivalent to
const dispaches = {
loadingOn: () => ({
type: "foo/loadingOn"
}),
loadingOff: () => ({
type: "foo/loadingOff"
})
};