How do I add an element to array in reducer of React native redux?

前端 未结 4 747
予麋鹿
予麋鹿 2020-12-22 16:52

How do I add elements in my array arr[] of redux state in reducer? I am doing this-

import {ADD_ITEM} from \'../Actions/UserActions\'
const ini         


        
4条回答
  •  北海茫月
    2020-12-22 17:24

    If you need to insert into a specific position in the array, you can do this:

    case ADD_ITEM :
        return { 
            ...state,
            arr: [
                ...state.arr.slice(0, action.pos),
                action.newItem,
                ...state.arr.slice(action.pos),
            ],
        }
    

提交回复
热议问题