未处理的拒绝(错误):给定的操作返回未定义的 React JS
2018-05-31
4206
我不确定为什么会收到错误。未处理的拒绝(错误):给定操作“SET_CARS”,reducer“cars”返回未定义。要忽略操作,您必须明确返回之前的状态。如果您希望此reducer不保存任何值,则可以返回null而不是未定义。
REDUCERS
import { SET_CARS} from "../actions";
import { combineReducer } from "redux";
function cars(state = [], action) {
switch (action.type) {
case SET_CARS:
return
action.items;
default:
return state; }
}
const rootReducer = combineReducers({
cars
});
export default rootReducer;
ACTIONS
export const SET_CARS = 'SET_CARS';
export function setCars(items){
return {
type: SET_CARS ,
items
}
}
SearchCars
class SearchCars extends Component {
constructor() {
super();
this.state = {
tansmition: ""
};
}
search() {
let { tansmition } = this.state;
const url = `http://localhost:4000/vehicles/?
transmission=${tansmition}`;
fetch(url, {
method: 'GET'
})
.then(response => response.json())
.then(json => {
this.props.setCars(json.results)
})
}
2个回答
您的
fetch
在其粗箭头函数中缺少
return
,因此 fetch 未返回任何内容。
将此行:
.then(json => {
this.props.setCars(json.results)
})
更改为:
.then(json => {
return this.props.setCars(json.results)
})
AndrewL64
2018-05-31