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
}
Source: stackoverflow.com
Related Query
- React Hooks: Instantiating state hooks on validation Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- React hooks in react library giving Invalid hook call error
- I have this React Invalid Hook Call Error that I'm getting and I wanted to see if I was breaking the rules of hooks or is it something else?
- Cant use Hooks with my React Component Library. Invariant Violation: Invalid hook call
- React useContext throws Invalid hook call error
- React Hooks won't work on Firebase Cloud Functions Error: Invariant Violation: Invalid hook call
- React hooks in Gatsby: Invalid hook call
- React invalid hook call error and storybook 6
- TypeScript React Invalid Hook Call Error ("Error: Invalid hook call.")
- Invalid hook call error react-redux hooks
- React hook, Invalid hook call error occurs
- Getting Invalid hook call error on importing a component in React
- why react router dom throwing hooks Invalid hook call error?
- React Redux-Toolkit: Invalid hook call - Hooks can only be called inside of the body of a function component
- React call from class to functional component. Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- Invalid hook call error occurs in Stripe + React
- Invalid Hook Call Error when using react useState hook inside firebase app
- Getting invalid hook call error in React when I used materidal design bootstrap component in my application
- Invalid hook call error when using React function component from npm package
- React error invalid hook & state is not defined
- Invalid hook call - useContext - React Hooks
- Invalid hook call react hooks
- Invalid hook call error when showing/hiding component with React Redux
- Converting class to function React Error invalid hook call
- Hooks error: Invalid Hook Call using NextJS or ReactJS on Windows
- react-intl with react-testing-library gives Invalid hook call error
- Invalid hook call for using useHistory, useLocation hooks
- React bootstrap - Invalid hook call
- React navigate router v6 invalid hook call
- Access old state to compare with new state inside useEffect react hook with custom hooks usePrevious
More Query from same tag
- How to re-render immediate after on-click?
- Tag <ErrorMessage> Formik react Style
- TypeError: Cannot read property 'value' of undefined (d is undefined)
- react xarrows not render properly when site load first time
- React.js - Sortable Tree: How To Add Custom Child Data To React-Sortable-tree?
- How to change the selected table row background using Material-UI on React
- Cannot read properties of undefined (reading '0')
- Set timeout for message with hooks
- Pass classname to material ui date-picker dialog
- How do I add an HTML page for template tags when I am using Routes to display web app pages
- NextJS Dynamic Routing, re-render specific Child Components
- Typescript React stateless function with generic parameter/return types
- Handling state value in If condition in React JS
- Array Find() method is always returning 'undefined' while searching data with respect to URL parameters
- How to set a state varibale for a dynamically rendered component in React
- Drag and Drop with resizing browser window (react-use-gesture)
- Props undefined inside the return but otherwise defined
- React app runs locally, crashes when on Heroku error code=H10
- How can handle not found page for nested routing in react router v4?
- Re-rendering only updated elements on react form
- How to prevent the rerenders and improve performance (Todo example with hooks)
- clearInterval not stopping my counter in useEffect
- TaskHarness in React Storybook
- Why does window.location not work in my react component?
- npm run build > bundle.js not getting created
- Error with terminal when trying to create-react-app
- How to integrate CoreUI/react with typescript
- How to apply the style of the rest of the items when I click item in REACT?
- how to add a new field in row based on some value match on dropdown selection
- react router - unable to pass store down