2017-03-08 1 views
1

ich meine redux FunktionenUmgestalten Variablen in redux Funktionen

In diesen drei Fällen Refactoring in am Umbenennung der Variable in einer anderen Art und Weise, und ich will das nicht

Siehe Beispiel

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; 

Dies sind die verschiedenen Variablennamen in den drei Fällen

const index = action.index; 

const a = action.index; 

const count = action.index; 

Same für

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

let string2 = state[count].viewBox; 

und

let array = string.split(" "); 

let array2 = string2.split(" "); 

Wie kann ich wieder verwenden denselben Variablennamen für die alle drei Fälle?

Ich meine den gleichen Namen für die drei verschiedenen Fällen unter Verwendung von wie:

const index-let string - let array

Antwort

5

Sie einen Umfang von Fall zu Fall hinzufügen, können sie in geschweiften Klammern Verpackung:

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 
}