【问题标题】:trying fo delete a specific element in a list react-native expo试图删除列表中的特定元素 react-native expo
【发布时间】:2021-01-22 08:42:56
【问题描述】:

伙计们,我正在尝试使用 react-native expo 从列表中删除一个元素 但它一直说delete_post 不起作用

const ReduceBlogPost = (state, action) => {
  switch (action.type) {
    case 'delete_post':
      return state.filter((blogs) => blogs.id !== action.playload);
    case 'add_Blog_Post':
      return [...state, { id: Math.floor(Math.random() * 9999), title: `blog number one #${state.length + 1}` }];
    default:
      return state;
  }
};
export const BlogProvider = ({ children }) => {
  const [blogs, dispatch] = useReducer(ReduceBlogPost, []);
  // updateBlog
  const updateBlog = () => {
    dispatch({ type: 'add_Blog_Post' });
  };
  //delete blog post
  const delete_blog = (dispatch) => {
    return (id) => {
      dispatch({ type: 'delete_post', playload: id });
    };
  };
  return <BlogContext.Provider value={{ data: blogs, updateBlog }}>{children}</BlogContext.Provider>;
};

【问题讨论】:

    标签: reactjs react-native expo react-native-elements


    【解决方案1】:

    为什么要在delete_blog 函数中传递参数dispatch?您已经拥有从useReducer 获取的dispatch。 你可以喜欢这个

    //delete blog post
      const delete_blog = (id) => {
        dispatch({ type: 'delete_post', playload: id });
      };
    

    我认为这取决于您的上下文。

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 2020-05-27
      • 2021-02-02
      相关资源
      最近更新 更多