【发布时间】:2017-06-29 23:23:33
【问题描述】:
我正在使用一些 svg 元素,并且我已经构建了一个增量按钮。
增量按钮采用path d 值将其转换为数组,并从数组的第三个元素中求和5。如果元素是 'L' 或 'M' 它什么都不做
这里是redux中的click函数,它取自增action state的值
const a = action.index;
let string = state[a].d;
转换成数组
let array = string.split(" ");
然后它会完成我在循环中所说的所有计算
查看完整的 redux 功能
switch(action.type) {
case 'INCREMENT_COORDINATES' :
console.log("incrementing coordinaates")
const a = action.index;
let string = state[a].d;
let array = string.split(" ");
let max = array.length;
let i = 3;
let click = () => {
i++;
if ( i === max ) i = 3;
if (array[i] !== 'M' && array[i] !== 'L') array[i] = parseInt(array[i]) + 5;
array.join(' ');
};
return [
...state.slice(0,a), // before the one we are updating
{...state[a], d: click()}, // updating the with the click function
...state.slice(a + 1), // after the one we are updating
]
default:
return state;
}
一边调试一边观看视频演示http://www.fastswf.com/uSBU68E
如您所见,代码进行了正确的计算,它将第四个元素的 5 相加,并返回数字 331。一切都很好!但问题是我需要将其返回为'331',这是数组的一个新元素以继续循环。
我已经构建了一个演示,它使用相同的代码完美地工作并完成了我想要实现的目标!所以当我在我的 redux 函数中应用它时,我不明白我做错了什么。
查看jsBin 完全按照我的意愿工作。
【问题讨论】:
标签: javascript arrays reactjs svg redux