Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.includes() is not a function

It works for the first click, but when i click it again to unselect it, it shows me error :

state.selectedStudents.includes is not a function. (In 'state.selectedStudents.includes(action.item)', 'state.selectedStudents.includes' is undefined)

import {
  SELECT
} from "../actions/postActionTypes";

const initialState = {
  students: ['Aditya', 'Rohan', 'Hello'],
  selectedStudents: []
}

const selectReducer = (state = initialState, action) => {


  switch (action.type) {
    case SELECT:
      return {
        ...state,
        selectedStudents: state.selectedStudents.includes(action.item) ? state.selectedStudents.splice(state.selectedStudents.indexOf(action.item), 1) : state.selectedStudents.push(action.item)
      }
      default:
        return state;
  }

}

export default selectReducer;

like image 533
kvadityaaz Avatar asked Sep 02 '19 10:09

kvadityaaz


People also ask

What is the function of the includes () in arrays?

The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

Can you use includes () with an array in JavaScript?

includes() You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

Is not a function at array some?

The "some is not a function" error occurs when the some() method is called on a value that is not an array. To solve the error, convert the value to an array before calling the method or make sure to only call the some() method on valid arrays. Here is an example of how the error occurs.

Can an array contain functions?

The simple answer is yes you can place function in an array. In fact, can declare variables and reference them in your function.


2 Answers

First of all state.selectedStudents.includes is not a function. means that state.selectedStudents is not an array. so what is it?

.push() doesn't return the array, it returns the length of the array after push. based on MDN:

Array.push() return value: The new length property of the object upon which the method was called.

So after the first SELECT action, your state chages to this:

state = {
  students: ['Aditya', 'Rohan', 'Hello'],
  selectedStudents: 1, // <- it's a number, not an array.
}

And the second time you fire SELECT action, state.selectedStudents.includes(action.item) throws and error because state.selectedStudents is 1 which isn't an array.

Change your switch case to this:

switch (action.type) {
  case SELECT:
    return {
      ...state,
      selectedStudents: state.selectedStudents.includes(action.item) ?
        state.selectedStudents.filter(student => student.id !== action.item.id) :
        [...state.selectedStudents, action.item] // <- no mutation, creates a new array.
    }
  default:
    return state;
}
like image 80
Ahmad Maleki Avatar answered Sep 24 '22 14:09

Ahmad Maleki


state.selectedStudents.push(action.item)

returns the new length of the array and is not a list. so the next time you try it the type number does not have the includes method.

like image 34
Amin Bashiri Avatar answered Sep 23 '22 14:09

Amin Bashiri