react redux 에서 switch 문 사용할때 주의사항
3077 단어 switchreact reduxreact redux
reducer 함수를 정의할때 action.type 을 분별하기 위해 switch 문을 사용하는 경우가 있다.
이 경우 초기의 상태, 즉 아무런 action 을 취하지 않았을 때의 return 값을 정해줘야 하는데
올바른 예시
...
case action.type == DELETE_TODO :
return{
...state,
//해당 id 의 객체를 제외한 나머지 배열을 반환
todos: todos.filter(todo => todo.id !== action.id)
}
default:
return initialState
}
}
올바르지 않은 예시
객체안에 또 현재의 state 객체를 감싸면 안된다. return null 로 적는 실수도 조심하자.
...
case action.type == DELETE_TODO :
return{
...state,
//해당 id 의 객체를 제외한 나머지 배열을 반환
todos: todos.filter(todo => todo.id !== action.id)
}
default:
return { initialState }
//return null
}
}
Author And Source
이 문제에 관하여(react redux 에서 switch 문 사용할때 주의사항), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hyunn/react-redux-에서-switch-문-사용할때-주의사항저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)