用splice()删除项目

时间:2019-09-19 14:46:55

标签: javascript reactjs

我在组件中显示一些通过API调用和.map方法获得的电影。当我尝试将某个项目删除到此电影列表中时,仅对最后一个项目进行删除。 我使用splice()方法,并为我的deleteMovieFromFav()函数提供了索引。 我不知道我在想什么.. 有人知道怎么做吗?

删除电影的功能:

deleteMovieFromFav = (idx) => {
    let { movies } = this.props.movieReducer;
    movies.splice(idx, 1)
    this.setState({movies: [...movies]})
}

onClick删除电影:

{
 this.props.movieReducer.movies.map((movie, idx) => {
  return(
   <div key={idx}>
    <Snackbar
      action={[
       <Button key="undo" color="secondary" size="small" 
        onClick={() => {
         this.deleteMovieFromFav(idx)
         this.handleCloseAlert()
        }}> Yes
       </Button>,
       <Button 
        key="no" 
        color="secondary" 
        size="small" 
        onClick={() => this.handleCloseAlert()}>
      ]}
    />
  </div>
 )
})
}

1 个答案:

答案 0 :(得分:0)

我使用了redux,并创建了一个用于从收藏夹中删除电影的动作,现在它是第一个被删除的元素。我希望我的deleteMovie动作对添加为收藏夹的电影的ID起作用但是我只能在TreeNode* buildTree(vector<int>& inorder, int is, int ie, vector<int>& postorder, int ps, int pe) { if (is > ie || ps > pe) return nullptr; struct TreeNode root(postorder[pe]); int index = getIndex(inorder, postorder[pe]); //cout<< index <<" "<<postorder[pe]<< endl; root.left = buildTree(inorder, is, index - 1, postorder, ps, ps + index - is - 1); root.right = buildTree(inorder, index + 1, ie, postorder, pe - ie + index,pe - 1); //cout<< root.val << " " << root.right << " " << root.left <<endl; return &root; } 中访问此ID,而不能在redux中访问。

.map此处:

movieReducer

import { DELETE_MOVIE } from '../actionType/movies' const initialState = { movies: [] }; export default (state = initialState, action) => { switch(action.type){ case DELETE_MOVIE: let selectedMovies = state.movies selectedMovies.splice(action.payload, 1) return {...state, movies: [...selectedMovies]}; default: return state; } } 此处:

movieActions