score:0

make sure that all your hook (usestate, useeffect...) are inside the checkuserexists function

score:0

are you doing const validateregister = () =>{} to wrap all what you post? it seems that you are not using a react component, or you are trying to use a component as a function util, so you cant use const [errors, seterrors] = usestate({}); you can create a custom hook called usecheckuserlist, with the error and seterror state and return the checkuserexists and the error state, and import it to your component.

import {usestate} from 'react'

export const usecheckuserlist = () => {

  const [errors, seterrors] = usestate({});

    const checkuserexists = async (phone) => {

        console.log('inside checkuserexists')

        let isuserexist = false;

        let formdata = new formdata();
        formdata.append('phone', phone);

        const response = await axios.post('http://localhost:3001/doesuserexistbyphone', formdata).then(response=>{
            isuserexist = false;
            console.log('success response: ', response)
        }).catch(errorresponse=>{
            console.log('error response: ', errorresponse)
            isuserexist = true;
            seterrors(
               ...errors, {phone: 'user already exist, u know?'}
            )
        })
        
        return isuserexist;
        
     }



    return {errors, checkuserlist};
    
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

and in the file where you are going to use it just import the usecheckuserlist, and do the following: const {errors, checkuserlist} = usecheckuserlist() now you can youse the checkuserlist function and errors state.

this is just an example, you can also take all your code into a custom hook and then use it in the validateregister component.

score:0

you have to pass "error and seterror" to your validateregister component from where you called or import that component like:

<validateregister error={error} seterror={seterror} />

// and take it as props in validateregister component like

const validateregister = ({error, seterror}) => {
 //your code
 }

Related Query

More Query from same tag