构建React Redux应用程序我想知道在哪里放置依赖选择逻辑。我检查了有关业务逻辑的问题,并提出了使用reducer,action或selector的建议。
选择器似乎最适合,但在更新存储之前我需要这个计算,这个用法是反模式。
App有我想通过动作创建者添加到商店的过滤器,但在添加到商店之前我想计算在商店更改后呈现的默认值。
我有一些依赖选择选项:
const fields = [
{value: "Name", title: "Name", type: "string"},
{value: "Date", title: "Date", type: "date"},
{value: "DateTime", title: "DateTime", type: "datetime"},
{value: "Alive", title: "Alive", type: "check"}
];
const conditions = [
{value: "is", title: "Is", types: ["string"]},
{value: "contains", title: "Contains", types: ["string"]},
{value: "from", title: "From": types: ["date", "datetime"]},
{value: "between", title: "Between": types: ["date", "datetime"]},
{value: "checked", title: "Checked": types: ["check"]},
];
生命周期是:
1. Get first available field
2. Get first available condition for this field (field.type exists in conditions.types)
3. Set/Format default value according to field and condition
4. Add to store and render in UI
当我向商店添加过滤器时,我希望获得如下设置的默认值:
[{
field: "DateTime", // <-- Calculated default value
condition: "Between", // <-- Calculated default value
// Value property structure is calculated as well
value: [{
dateStart: "<There goes calculated default value>",
timeStart: "<There goes calculated default value>",
dateEnd: "<There goes calculated default value>",
timeEnd: "<There goes calculated default value>",
}]
},{
field: "Name", // <-- Calculated default value
condition: "Contains", // <-- Calculated default value
// Value property structure is calculated as well
value: [{
string: "<There goes calculated default value>",
}]
}]
将这种逻辑放入动作创建者或减速器或其他地方的哪个地方最好?哪种方法/插件最适合?