Create an async function inside your effect that wait the getData(1)
result then call setData()
:
useEffect(() => {
const fetchData = async () => {
const data = await getData(1);
setData(data);
}
fetchData();
}, []);
More Related Contents:
- State not updating when using React state hook within setInterval
- Why do we need middleware for async flow in Redux?
- Make React useEffect hook not run on initial render
- Push method in React Hooks (useState)?
- React useEffect in depth / use of useEffect?
- Wrong React hooks behaviour with event listener
- Reactjs async rendering of components
- What the difference of this.state and this.setstate in ReactJS?
- How to call loading function with React useEffect only once
- How to async await in react render function?
- How to use componentWillMount() in React Hooks?
- Is it possible to share states between components using the useState() hook in React?
- react useEffect comparing objects
- React useState hook event handler using initial state
- What’s the difference between `useRef` and `createRef`?
- React useEffect causing: Can’t perform a React state update on an unmounted component
- How does JavaScript mechanism behind react hooks work?
- What is useState() in React?
- React – useState – why setTimeout function does not have latest state value?
- What does useCallback/useMemo do in React?
- React Hooks useState+useEffect+event gives stale state
- How to add “refs” dynamically with react hooks?
- Do we still need functional setState way in react hooks?
- Proper way of using React hooks + WebSockets
- Uncaught Invariant Violation: Rendered more hooks than during the previous render
- React Hook “useEffect” is called conditionally
- What are the differences when re-rendering after state was set with Hooks compared to the class-based approach?
- Performance penalty of creating handlers on every render with react-hooks
- Function inside functional component in React hooks – Performance
- Why would we use useEffect without a dependency array?