"Is assigned a value but never used" Redux 减速器中的警告
"Is assigned a value but never used" warning in Redux reducer
这是我的代码
import * as actionTypes from '../actions/actionTypes';
const counterReducer = (state = 0, action) => {
let newState;
switch (action.type) {
case actionTypes.INCREASE_COUNTER:
return (newState = state + action.payload);
case actionTypes.DECREASE_COUNTER:
return (newState = state - action.payload);
case actionTypes.INCREASE_BY_TWO_COUNTER:
return (newState = state + action.payload);
default:
return state;
}
}
export default counterReducer;
但我的终端显示 'newState' 已分配一个值但从未使用过。我错过了什么?
您不需要 newState
变量,它在您使用它的方式中毫无用处。
没有它你可以得到相同的结果。
return 语句将 return 结果并结束执行而不需要使用变量。
import * as actionTypes from '../actions/actionTypes';
const counterReducer = (state = 0, action) => {
switch (action.type) {
case actionTypes.INCREASE_COUNTER:
return state + action.payload;
case actionTypes.DECREASE_COUNTER:
return state - action.payload;
case actionTypes.INCREASE_BY_TWO_COUNTER:
return state + action.payload;
default:
return state;
}
}
export default counterReducer;
这是我的代码
import * as actionTypes from '../actions/actionTypes';
const counterReducer = (state = 0, action) => {
let newState;
switch (action.type) {
case actionTypes.INCREASE_COUNTER:
return (newState = state + action.payload);
case actionTypes.DECREASE_COUNTER:
return (newState = state - action.payload);
case actionTypes.INCREASE_BY_TWO_COUNTER:
return (newState = state + action.payload);
default:
return state;
}
}
export default counterReducer;
但我的终端显示 'newState' 已分配一个值但从未使用过。我错过了什么?
您不需要 newState
变量,它在您使用它的方式中毫无用处。
没有它你可以得到相同的结果。
return 语句将 return 结果并结束执行而不需要使用变量。
import * as actionTypes from '../actions/actionTypes';
const counterReducer = (state = 0, action) => {
switch (action.type) {
case actionTypes.INCREASE_COUNTER:
return state + action.payload;
case actionTypes.DECREASE_COUNTER:
return state - action.payload;
case actionTypes.INCREASE_BY_TWO_COUNTER:
return state + action.payload;
default:
return state;
}
}
export default counterReducer;