score:2
You're trying to call a hook within a loop. This is a bad practice and is not allowed. Hooks should always be called at the top level.
You're also using a predefined hook for data retrieval. This hook is meant to do one fetch. In your particular order, you need to do this in a loop operation, which means the hook you're using doesn't fit your need.
What you can do, is create a function that does the fetching instead of using the hook. I like to use axios.
const getPeriods = async (offset, slug) => {
const url = '/company/' + slug + '/waterfall?colIndexOffset=' + offset
const response = await axios.get(url)
return response.data
}
In this function, you are fetching 1 item. You may rewrite it so that you do the looping inside of it, or you can call it inside a loop
for (let i = 0; i < length; i++) {
my_array.push(await getPeriods(i, slug)
}
Note that the loop is using await
which means it needs to be inside an async function. If you don't want that, you can always do something like this:
getPeriods(i, slug).then((res) => {
my_array.push(res.data)
}
Hope this helps solving your issue.
Source: stackoverflow.com
Related Query
- How to useFetch with a for loop?
- How do I create a select box with a for loop in React?
- How to loop through an array with keys and values in to send a request for Axios.get's params in ReactJS?
- how to update html content dynamicaly with for loop in react js javascript
- How can I loop through arrays for different users in a database and generate users with same dates?
- How do I change a variable in a for loop with a onclick ? React jsx file
- How can I use multiple refs for an array of elements with hooks?
- How to specify (optional) default props with TypeScript for stateless, functional React components?
- How to test style for a React component attribute with Enzyme
- How do I use for loops with react?
- How to setup Material-UI for React with Typescript?
- How to specify function parameters for React component callback with TypeScript?
- How to run Jest tests with coverage for one file
- How to define schema for recursive model with Normalizr
- React Router with - Ant Design Sider: how to populate content section with components for relevant menu item
- How to use typescript with the definition of custom styles for a select using react-select
- How to set a test for multiple fetches with Promise.all using jest
- How to use React Hooks Context with multiple values for Providers
- How add style for child elements with react-jss component?
- How to write tests for Formik-powered input components with Jest & Enzyme (useField Hook)?
- How to integrate PWA with Next.js to allow for notifications and push notifications
- How can I use CSS @media for responsive with makeStyles on Reactjs Material UI?
- How to implement the OAuth2 Authorization code grant with PKCE for a React single page application?
- Javascript / webpack : how to concatenate all json files in directory with a custom loop over files
- Next.js: How can I use getStaticProps/getStaticPaths for sites with multiple domains/hostnames?
- How to use a proxy for a link with reactjs
- How to wait for setState in componentDidMount to resolve when testing with enzyme?
- How to correctly wait for Translation with react-i18next
- How to dynamically update the value for any input field with one event handler function, using hooks
- How to recreate auto-collapasable menu with semantic-ui for react?
More Query from same tag
- Can't have my data from firebase after get method with reactjs
- Difference between register and setRegister in useEffect dependency
- react-boostrap-typeahead reset with formik
- How to make React Component call a function on every state change?
- Create component directory with all necessary files in a few clicks
- Render Modal for Dynamic Table in React
- Openlayers React rotate polygon feature
- Can I turn off create-react-app chunking mechanism?
- this.setState in handleSubmit function not resetting form inputs
- "SyntaxError: Unexpected token < in JSON at position 0"
- React: Making width of div equal to height. The height is not being calculated correctly
- styled-components not responding
- How to get multiple refs with array in React and useRef hook?
- React - Get property of a component inside its own declaration to use its value in another property?
- Is there a way to terminate and restart a new web socket connection onchange of props?
- How to use webpack with create-react-app?
- MUI v5: Do I need to install @emotion/react or @emotion/styled to use sx prop?
- Unexpected token when typing a function with destructuring assignment parameters in Create-React-App
- How to extract hashtags in URL?
- Typescript error after update of react and react-dom
- How to re-render this.state after the server goes offline
- React check authentication on startup
- How to convert a plain JS object to an Immutable collection?
- Material UI - body scroll locked
- Using React, what is an easy way to load files based off clicking buttons with the same name?
- React componet should be written as a pure function
- cannot GET /PATH error when building NUXT app - DOT in the query params
- How do I correct my htaccess for proxying search engine crawl requests?
- How to use custom font special symbols in object properties used in React Component
- Clearing dynamically added input fields