未处理的拒绝(错误):给定操作“SET_CARS”,reducer“cars”返回undefined。要忽略某个操作,您必须显式返回先前的状态。如果您希望此reducer不保留任何值,则可以返回null而不是undefined。我如何解决这个问题我不知道我哪里出错了?
动作:index.js
export const SET_CARS = 'SET_CARS';
export function setCars(items){
return {type: SET_CARS,
items}
}
import React, { Component} from "react";
import { Form, FormGroup,FormControl,Button } from "react-bootstrap";
import { connect } from "react-redux";
import { setCars } from "../actions";
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 => {
return this.props.setCars(json.results)
})
}
render() {
return ( <Form inline >
< FormGroup >
< FormControl type = "text" placeholder = "Automated"onChange = { event => this.setState({ tansmition: event.target.value })
}/>
</FormGroup>
<Button onClick = {
() => this.search()}> Submit </Button>
</Form>
);
}
}
export default connect(null, {setCars})(SearchCars);
Reducers:index.js
import {SET_CARS} from "../actions";
import {combineReducers} 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;