reactjs - Remove value from array in Reducer -


so have following code. if action active, itll store activity type in array called selectedactivitytype. however, trying remove activity type if action not active.

any ideas:

const initialuserstate = {   selectedactivitytype: [], }  export const activitytypes = (state = selectedactivitytype, action) => {   switch (action.type) {   case types.diary_update_activity_type:     if(action.active) {       return {         ...state,         selectedactivitytype: [...state.selectedactivitytype, action.activitytypeid],       }     } else {       return {         ...state,        }     }   default: return state   } } 

i've taken code here , modified use in snippet examples.

the below snippet illustrate problem. current setup push value array if exists. meaning not have unique values in array. run snippet below , see duplicate entry.

let state = {    selectedactivitytype: [ ]  };    const activitytypes = (state = selectedactivitytype, action) => {    switch (action.type) {      case "update_activity_type":        return {         ...state,          selectedactivitytype: [...state.selectedactivitytype, action.activitytypeid]        }      default: return state      }  }    const addselectedactivitytype = (activitytypeid) => {    return {      type: "update_activity_type",      activitytypeid    }  };    state = activitytypes(state, addselectedactivitytype("testid"));  state = activitytypes(state, addselectedactivitytype("testid"));    console.log(state);

what can make helper function use in our reducer. if run below snippet see though tried add same value twice, end result 1 entry.

let state = {    selectedactivitytype: [ ]  };    const activitytypes = (state = selectedactivitytype, action) => {    switch (action.type) {      case "update_activity_type":              if ( !existsinarray(              state.selectedactivitytype,              action.activitytypeid           ) ) {             return {             ...state,              selectedactivitytype: [...state.selectedactivitytype, action.activitytypeid]            }           } else {             return state;           }      default: return state      }  }    const addselectedactivitytype = (activitytypeid) => {    return {      type: "update_activity_type",      activitytypeid    }  };    state = activitytypes(state, addselectedactivitytype("testid"));  state = activitytypes(state, addselectedactivitytype("testid"));    console.log(state);    // helper function    function existsinarray( array, value ) {    let i;    const length = array.length;    ( = 0; < length; i++ ) {      if ( array[ ] === value ) return true;    }    return false;  }

now know entries in array unique can remove entries array helper function. view below snippet gist!

let state = {    selectedactivitytype: [ "testid", "anotherid" ]  };    const activitytypes = (state = selectedactivitytype, action) => {    switch (action.type) {      case "remove_activity_type":        removefromarray(          state.selectedactivitytype,          action.activitytypeid        );                return state;      default: return state      }  }    const removeselectedactivitytype = (activitytypeid) => {    return {      type: "remove_activity_type",      activitytypeid    }  };    console.log( state );    state = activitytypes(state, removeselectedactivitytype("testid"));    console.log(state);    // helper functions    function existsinarray( array, value ) {    let i;    const length = array.length;    ( = 0; < length; i++ ) {      if ( array[ ] === value ) return true;    }    return false;  }    function removefromarray( array, value ) {    let index = array.indexof( value );      if ( index > -1 ) {      array.splice( index, 1 );    }  }

hope helps. ask questions if need to.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -