sourcecode

eslint: 대소문자 구분 없음 - 대소문자 블록의 예기치 않은 어휘 선언

copyscript 2023. 2. 15. 22:09
반응형

eslint: 대소문자 구분 없음 - 대소문자 블록의 예기치 않은 어휘 선언

리듀서 내부에서 이 컨텍스트에서 상태를 갱신하는 가장 좋은 방법은 무엇입니까?

case DELETE_INTEREST:
    let deleteInterests = state.user.interests;
    let index = deleteInterests.findIndex(i => i == action.payload);
    deleteInterests.splice(index, 1);
    return { ...state, user: { ...state.user, interests: deleteInterests } };

ESLint는 환원기 내부의 대소문자 블록에 문장을 삽입하는 것을 좋아하지 않습니다.이것에 의해, 다음과 같은 것을 얻을 수 있습니다.

eslint: 대소문자 구분 없음 - 대소문자 블록의 예기치 않은 어휘 선언

ESLint는 환원기 안에 대소문자를 넣는 것을 좋아하지 않습니다. 왜일까요?

이는 변수가 현재 범위를 벗어나게 되므로 권장되지 않습니다.case. 블록을 사용하면 변수의 범위를 해당 블록으로 제한할 수 있습니다.

사용하다{}대소문자를 사용하여 블록 스코프를 작성하려면 다음과 같이 하십시오.

case DELETE_INTEREST: {
    let .....
    return (...)
}

다음 스니펫을 확인합니다.

function withOutBraces() { 
  switch(1){
    case 1: 
      let a=10; 
      console.log('case 1', a); 
    case 2: 
      console.log('case 2', a)
  } 
}

function withBraces() { 
  switch(1){
    case 1: {
      let a=10; 
      console.log('case 1', a); 
    }
    case 2: {
      console.log('case 2', a)
    }
  } 
}

console.log('========First Case ============')
withOutBraces()
console.log('========Second Case ============')
withBraces();

요소를 배열에서 삭제하려면 array를 사용합니다.스플라이스가 원래 배열의 변경을 수행하기 때문에 필터가 필요합니다.다음과 같이 적습니다.

case DELETE_INTEREST:
    let deleteInterests = state.user.interests;
    let newData = deleteInterests.filter(i => i !== action.payload);
    return { ...state, user: { ...state.user, interests: newData } };

다음과 같이 {}을(를) 사용하여 케이스 내부를 캡슐화해 보십시오.

      case EnumCartAction.DELETE_ITEM: {
           const filterItems = state.cart.filter((item) => item._id !== action.payload)
           return {
                ...state,
                cart: filterItems
           }
      }

간단한 수정은 브래킷을 사용하는 것입니다.{}케이스 코드를 캡슐화합니다.사용하시는 경우return추가가 필요할 수 있습니다.break경우에 따라서는.

언급URL : https://stackoverflow.com/questions/50752987/eslint-no-case-declaration-unexpected-lexical-declaration-in-case-block

반응형