Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Rejection (Error): Given action returned undefined React JS

I am not sure why I get the error. Unhandled Rejection (Error): Given action "SET_CARS", reducer "cars" 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.

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)
  })
}

enter image description here

like image 303
Sambulo Senda Avatar asked Dec 01 '25 06:12

Sambulo Senda


1 Answers

ASI strikes again.

return 
action.items;

This gets interpreted as:

return;
action.items;

Put them together on one line and it should work. I'd also consider using a formatter like prettier. It'll help yourself and others looking at your code to see bugs like this more easily.

like image 193
kingdaro Avatar answered Dec 03 '25 20:12

kingdaro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!