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


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'http://localhost:3001/doesuserexistbyphone', formdata).then(response=>{
            isuserexist = false;
            console.log('success response: ', response)
            console.log('error response: ', errorresponse)
            isuserexist = true;
               ...errors, {phone: 'user already exist, u know?'}
        return isuserexist;

    return {errors, checkuserlist};
<script src=""></script>
<script src=""></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.


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