事件.jsimport React, { useEffect, useState } from 'react';import { useDispatch, useSelector } from 'react-redux';import { Link, useHistory } from 'react-router-dom';import { addEvent } from '../actions/event';const AddEvent = () => {let history = useHistory();const [event, setEvent] = useState();const createEvent = useSelector(state => { console.log(state) return ( state.addEvent )});const { loading, newEvent, success, error } = createEvent;const handleChange = e => { setEvent({ ...event, [e.target.name]: e.target.value });};const dispatch = useDispatch();useEffect(() => { if(success) { history.push('/') =========>>>>>>>> It work once fine but again when I tries to open add Event it automatically redirects to '/' as it is not getting reset after redirecting };},[success]);const submitHandler = (e) => { e.preventDefault(); dispatch(addEvent(event));};return ( <div> { loading ? ( <div> Loading... </div> ) : error ? ( <div> {error} </div> ) : ( <form onSubmit={e => submitHandler(e)}> <div className="form-group"> <label htmlFor="name" className='mt-2'>Name</label> <input type="text" className="form-control" id="name" name="name" onChange={e => handleChange(e)} /> </div> <div className="form-group"> <label htmlFor="description">Description</label>我正在學習如何使用 React Redux,所以我嘗試 CRUD 工作流程工作正常,但在成功添加事件后,我希望它重定向到主頁,它工作正常,但由于 React-Redux 保持其狀態(tài),因此它再次成功重定向到主頁設置為 true 所以我創(chuàng)建了另一個重置類型以將 success 設置為 false 但不知道如何在成功添加數(shù)據(jù)后每次調用
如何使用react hooks調用reducer中的重置類型
慕姐4208626
2023-08-05 20:51:35