在react js中使用reducer的axios删除操作

我正在使用上下文 api 进行 crud 操作并使用 reducer。我从 api 获取数据并将其存储为初始值。但现在我很困惑如何从我获取的列表中删除用户。我做了一个删除功能,它可以在手动刷新时正常工作,但不会自动刷新并返回错误。如何制作删除功能。

   const InitialState = {
  Users:  []
}

const Reducer = (state,action) =>
{
    switch(action.type)
    {
        case 'FETCH_USERS':
            return{
                Users: action.payload
            }
        case 'REMOVE_USER':
            return{

                Users: action.payload
            }
        default: 
        return state
    }
    
}
const GlobalContext = createContext(InitialState)

const GlobalState = ({children}) => {
    
    const [state,dispatch] = useReducer(Reducer,InitialState);

    useEffect(()=>
    {  
        fecthapi();
    },[])

    const fecthapi = async () =>
        {
           const res  = await axios.get('http://localhost:3002/users')
           dispatch({type: 'FETCH_USERS',payload: res.data})
        }
     
    const Remove = async  (id) =>
    {
        await  axios.delete(`http://localhost:3002/users/${id}`)
        dispatch({
        type: 'REMOVE_USER',payload: id
        
        })
       
    }
    return (
    <>
    <GlobalContext.Provider value = {{Users: state.Users,Remove: Remove}}>
        {children}
    </GlobalContext.Provider>
    </>
    )
}```
lt277602467 回答:在react js中使用reducer的axios删除操作

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/579446.html

大家都在问