首页 > 解决方案 > 我正在尝试删除一个项目,但每次单击按钮时都会返回错误“调度不是函数”

问题描述

我正在开发 CRUD 应用程序,我通过 API 获取数据,一切正常,除非我尝试删除项目。如果我点击按钮,它会返回一个错误:

dispatch 不是一个函数。

ADDUPDATE动作工作正常,而不是DELETE. 如果你想浏览这个项目并给我反馈,可以在Github上找到这个 repo 。

const Return = (props, {dispatch}) =>{

    const initialFormState = {
        id:props.id, studentID: '', booknum: props.booknum,
        rented: '', exist: ''
    }
    const[data, setData]=useState(initialFormState)
    const [modal, setModal] = useState(false);

    const handleFormState = e=>{
        const {name, value} = e.target
        setData({...data, [name]: value})
    }

    const toggle = () => setModal(!modal);


    const studentRented = (studentID, booknum) =>{
        let id = []
        let num = props.getRented.length
        props.getRented.filter((key) =>{
            return ((parseInt(key.studentID) === parseInt(studentID)) && (key.booknum == booknum))?
               id.push(key.id) :  num = num -1
          }) 
        return id 
    }

    const studentExist = (studentID)=>{
        let num = props.students.length
        props.students.filter((student) =>{
            return (parseInt(student.studentID) === parseInt(studentID))?
               null  : num = num - 1
          }) 
        return num 
    }

    const onSubmit=(e)=>{
        e.preventDefault();
        if(!data.booknum || !data.studentID){
            setState(() => ({ error: 'One of the field is empty' }));
        }else{
            const info ={
                studentID: data.studentID,
                booknum: data.booknum
            }
            let ids = studentRented(data.studentID, data.booknum)[0]  
            try{
               // this dispatch return an error "dispatch nit a function 
                (parseInt(studentExist(info.studentID)) === 0) ?
                    setState(() =>({exist: `This student does not exist`})):
                    (studentRented(info.studentID, info.booknum).length === 1)  ?
                    dispatch(removeRent({ids})):
                    setState(() =>({rented: `This student did not checkout this book`}))
            }catch(err){
                console.log({err: err});
            }                         
        }    
    }  
    //

    return(
        <div>
            <small color="danger" onClick={toggle}>RETURN</small>
        <Modal isOpen={modal} toggle={toggle} >
        <ModalHeader toggle={toggle}>Modal title.</ModalHeader>
            <ModalBody>
            <Form onSubmit={onSubmit}>              
                <FormGroup>
                {data.rented && <p className='error text-danger'>Rented:{data.rented}</p>}   
                {data.exist && <p className='error text-danger'>Exist:{data.exist}</p>}
                    <Label for="title">Student ID</Label>
                    <Input type="number"name="studentID"id="b" value={data.studentID} onChange={handleFormState} />
                </FormGroup>
                <FormGroup>
                    <Label for="title">ISBN</Label>
                    <Input type="text" name="isbn" id="isbn"palceholder="add ISBN" value={data.booknum}
                   onChange={handleFormState}/>
                </FormGroup>
                <FormGroup>
                    <Button color="dark" style={{marginTop: '2rem'}} block>Submit</Button>
                </FormGroup>
                </Form>
            </ModalBody>
        </Modal>
        </div>
    );

};

const mapStateToProps = (state) =>{
    return{
        getRented: state.rentReducer,
        students: state.studentReducer
    }
}

export default connect(mapStateToProps, {getRents, getStudents})(Return);

标签: reactjsapireduxcruddispatch

解决方案


React 函数式组件函数,但它们只接收一个参数,一个props对象。react-reduxconnectHOC 注入了一个dispatchprop 以及映射statedispatchto props

访问props对象

const Return = (props) => {
  ...
  props.dispatch(...some action object);
  ...

或在函数签名中解构

const Return = ({ booknum, dispatch, getRented, id, <... other props> }) => {
  ...
  dispatch(...some action object);
  ...

推荐阅读