trying fo delete a specific element in a list react-native expo

Viewed 46

Guys I ma trying to remove an element from a list using react-native expo But it keeps saying that delete_post is not function

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>;
};
1 Answers

Why are you passing the parameter dispatch in the delete_blog function? You have the dispatch already that you took from useReducer. You could like this

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

I think that it would work depending of your context.

Related