2017-03-08 5 views
1
내 REDUX 기능 리팩토링해야

이 3의 경우리팩터링 변수

다른 방법으로 변수 이름을 변경하고 그리고 난이

는 예를

case 'INCREMENT_LIKES' : 
    const index = action.index; 
    return [ 
    ...state.slice(0,index), // before the one we are updating 
    {...state[index], likes: state[index].likes + 1}, 
    ...state.slice(index + 1), // after the one we are updating 
    ] 
case 'INCREMENT_COORDINATES' : 
    const a = action.index; 
    let string = state[a].d || state[a].d2; 
    let array = string.split(" "); 
    let max = array.length; 
    let last = max - 2; 
    let i = (state[a].index || 3) + 1; 
    if (i === last) i = 3; 
    if (array[i] !== 'M' && array[i] !== 'L') array[i] = parseInt(array[i]) + 20; 
    return [ 
    ...state.slice(0,a), // before the one we are updating 
    {...state[a], d: array.join(' '), index: i, d2: array.join(' '), index: i }, // updating 
    ...state.slice(a + 1), // after the one we are updating 
    ] 
case 'INCREMENT_VIEWBOX' : 
    const count = action.index; 
    let string2 = state[count].viewBox; 
    let array2 = string2.split(" "); 
    let max2 = array2.length; 
    let i2 = (state[count].index || 2) + 1; 
    if (i2 === max2) i2 = 2; 
    array2[i2] = parseInt(array2[i2]) - 20; 
    return [ 
    ...state.slice(0,count), // before the one we are updating 
    {...state[count], viewBox: array2.join(' '), index: i2}, 
    ...state.slice(count + 1), // after the one we are updating 
    ] 
default: 
    return state; 
을 참조하지 않습니다에

이들은

const index = action.index; 

const a = action.index; 

const count = action.index; 

같은 3의 경우에 다른 변수 이름입니다

let string = state[a].d || state[a].d2; 

let string2 = state[count].viewBox; 

let array = string.split(" "); 

let array2 = string2.split(" "); 

얼마나 내가 모든 3 개의 경우에 같은 변수 이름을 다시 사용할 수 있습니까?

나는 같은 3 가지 경우에 같은 이름을 사용하여 의미 :

const index-let string - 당신은 중괄호에 그들을 배치하여 각각의 경우에 범위를 추가 할 수 있습니다

답변

5

let array :

case 'INCREMENT_LIKES' : { 
    const index = action.index; 
    return [ 
    ...state.slice(0,index), // before the one we are updating 
    {...state[index], likes: state[index].likes + 1}, 
    ...state.slice(index + 1), // after the one we are updating 
    ] 
} 

case 'SOME_OTHER_ACTION_TYPE' : { 
    console.log(index) // -> undefined 
}