-1

I am working on CRUD application and I get the data through API, Everything is working, except when I try to delete an item. If I click on the button it returns an error:

dispatch is not a function.

The ADD and UPDATE actions are working fine instead of DELETE. If you want to look through the project and give me a feedback, the repo is available on Github.

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);
xKobalt
  • 1,498
  • 2
  • 13
  • 19
Ma Diallo
  • 11
  • 2

1 Answers1

0

React functional components are functions, but they receive only one parameter, a props object. react-redux's connect HOC injects a dispatch prop as well as mapping state and dispatch to props.

Access off the props object

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

or destructure in the function signature

const Return = ({ booknum, dispatch, getRented, id, <... other props> }) => {
  ...
  dispatch(...some action object);
  ...
Drew Reese
  • 165,259
  • 14
  • 153
  • 181