阿罗哈!
我正在尝试使用FP技术更改我的reducer代码(ramda.js) 但我对Ramda.evolve()有一个问题。这是代码: (为简单起见,我添加了两个动作来发送)
/**
Global reducer:
Handles state management for global operations
**/
"use strict";
import initialState from "./initialState";
import * as R from "ramda";
import {
SET_MODE
} from "../constants/ActionTypes";
let { packages, ...globalState } = initialState;
//currying
const createReducer = (globalState, handlers) => (state = globalState, action) =>
R.propOr(R.identity, R.prop("type", action), handlers)(state, action);
const handlers = {
[SET_MODE]: (state, action) =>
R.evolve(
{
mode: action.mode,
directory: action.directory
},
state
),
[TOGGLE_LOADER]: (state, action) => R.assoc("loading", action.loading, state)
};
const reducer = createReducer(globalState, handlers);
export default reducer;
(R = Ramda)
R.assoc工作正常,但R.evolve没有..它没有返回具有新值的状态,它只返回默认状态。 我做错了什么?
http://ramdajs.com/docs/#evolve
http://ramdajs.com/docs/#assoc
提前致谢
答案 0 :(得分:3)
R.evolve不
这是evolve的签名
{k: (v → v)} → {k: v} → {k: v}
需要一本功能词典。您正在传递值词典。这应该工作(虽然是多余的)
R.evolve({
mode: () => action.mode,
directory: () => action.directory
}, state)
添加强>
如果您需要一次更新多个道具,可以使用merge
及其变体。