score:0
Basically, you can only use hooks at the top level of your component.
Take a look at the rules of hooks (including useEffect).
You should try to put your if
and for
loop inside the useEffect
.
score:2
You can use promise.all
to execute promises (api fetcher) and useEffect
hook from React.
useEffect(() => {
setLoading(true); // If you want to add loader or spinner till companies info fetched
const connectionsPromises = connections.map((connection) => {
return VolunteerApi.getCurrentCompany(connection);
});
Promise.all(connectionsPromises).then((companies) => {
setCompany(companies); // save companies
setLoading(false); // disable loader
});
}, [connections])
Promise.all resolves promises parallelly
Source: stackoverflow.com
Related Query
- Using React useEffect in a loop to fetch data from an API multiple times
- React useEffect looping many times when fetching data and setState using fetch API
- I'm fetching a problem when I'm trying to fetch data from an API by using useEffect in react
- Can't get multiple data from API using foreach loop with Fetch API get request
- React Hook useEffect : fetch data using axios with async await .api calling continuous the same api
- displaying data from fetch api using react
- I can't fetch data from the api using useEffect
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to prevent React Hook's useEffect from fetching data multiple times
- React JS fetch data from multiple API function
- Display data from API using react component and useEffect
- How to fetch sequence api calls where second api call need particular data from first api call result using react hooks?
- I'm trying to fetch data from a server using useEffect react hook
- React App: How to display data from api using fetch
- How to fetch data from api in React by using axois?
- Best way to fetch data from a REST api using react hooks and context for state management?
- How to fetch data from API for multiple items in React JS component
- loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT
- How to fetch data from Web API using React query?
- React useEffect infinite loop fetching data from an api
- How to fetch data from an Authenticated API in React using Axios?
- React error when trying to get data from api using useEffect hook
- Error: Objects are not valid as a React child (found: [object Promise]) in react while using fetch to collect data from an api
- Fetch data from api in createContext using Fetch api in React doesn't work
- Fetch data from multiple pages of an API in react native
- How to fix zero return data from itunes api using react fetch
- Fetch data using an id from an api but display an alternative text to address bar e.g. content_title in Next.js
- Express does not receive parameters via POST from React using Fetch API
- React useEffect infinite loop fetch data axios
- How to fetch and display data from Express API to a React app?
More Query from same tag
- Fluxible router parameter a route
- React failed to compile reactLifecyclesCompat error
- What gets updated in React?
- Why does my history.push work in one function but the other?
- Why am I getting Too many re-renders in my code and how to resolve it?
- Can not provide/test saga when using a reselect selector
- Convert react JSX object to HTML
- React state being set back to initial [] when socket.on() is triggered inside useEffect()
- Fetch request fails when I send POST request from React app to a Flask API
- html2canvas not showing form elements properly
- Environment variables in gatsby-browser.js
- react prevent children render ( ajax loader waiting for response )
- TypeScript error: Type '() => boolean' is not assignable to type 'boolean'
- Filter item on item list that is on state using useState and useEffect
- React router get currently active segment as param
- this.Setstate is not working correctly in reactJS
- TypeError: The "original" argument must be of type Function
- How to register an protocol/deep links to my Electron App?
- Clickable background container of SVG element in Gatsby
- Javascript array equality control and changing value
- accessing object in react with map
- Event listener is not getting removed
- My reactjs app didn't open in incognito tab or new browsers and show this errors
- If id doesn't match path='/admin/edit/:id' then it should go to NOTFOUND page in Reactjs
- How can I delete an entire json object from an array of json objects and write it out to file?
- How to properly update state in react in hooks?
- How to get javascript to work in React component
- How to fix Firebase 9.0 invalid API key
- import a module from node_modules with babel but failed
- Get key from an object in an array in react-native