React: reducer visibilityFilter returned undefined

Error: Given action SET_VISIBILITY_FILTER, reducer visibilityFilter returned undefined.

Posted by chanweiyan on July 11, 2020

报错信息

Error: Given action “SET_VISIBILITY_FILTER”, reducer “visibilityFilter” returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.

1
2
3
4
5
6
7
8
9
10
onClick
src/containers/FilterLink.js:12
   9 |
  10 | // mapDispatchToProps
  11 | const mapDispatchToProps = (dispatch, ownProps) => ({
> 12 |   onClick: () => dispatch(setVisibilityFilter(ownProps.filter))
  13 | });
  14 |
  15 | // export

错误原因

输入错误 SHO_COMPLETED VisibilityFilters.SHO_COMPLETED

1
2
3
<FilterLink filter={VisibilityFilters.SHO_COMPLETED}>
  禁用
</FilterLink>

解决方法

将 SHO_COMPLETED 改为 SHOW_COMPLETED

1
2
3
<FilterLink filter={VisibilityFilters.SHOW_COMPLETED}>
  禁用
</FilterLink>

项目地址

https://github.com/cwy007/todos_app