score:2
The useEffect
hook will run after your component renders, and it will be re-run whenever one of the dependencies passed in the second argument's array changes.
In your effect, you are doing console.log(value)
but in the dependency array you didn't pass value
as a dependency. Thus, the effect only runs on mount (when value
is still ""
) and never again.
By adding value
to the dependency array, the effect will run on mount but also whenever value
changes (which in a normal scenario you usually don't want to do, but that depends)
import "./styles.css";
import React, { useEffect, useState } from "react";
const App = () => {
const [value, setValue] = useState("");
function fetchHello() {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Hello World");
}, 1000);
});
}
const handleSetValue = async () => {
const hello = await fetchHello();
setValue(hello);
};
useEffect(() => {
const fetchData = async () => {
await handleSetValue();
console.log(value);
};
fetchData();
}, [value]);
return (
<div className="App">
<h1>{value}</h1>
</div>
);
};
export default App;
Not sure exactly what you need to do, but if you need to do something with the returned value from your endpoint you should either do it with the endpoint returned value (instead of the one in the state) or handle the state value outside the hook
import "./styles.css";
import React, { useEffect, useState } from "react";
const App = () => {
const [value, setValue] = useState("");
function fetchHello() {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Hello World");
}, 1000);
});
}
const handleSetValue = async () => {
const hello = await fetchHello();
// handle the returned value here
setValue(hello);
};
useEffect(() => {
const fetchData = async () => {
await handleSetValue();
};
fetchData();
}, []);
// Or handle the value stored in the state once is set
if(value) {
// do something
}
return (
<div className="App">
<h1>{value}</h1>
</div>
);
};
export default App;
Source: stackoverflow.com
Related Query
- How does React useState hook work with mutable objects
- How does react useEffect work with useState hook?
- How does React useState and useCallback hook work when useCallback lacks dependencies
- Why does useEffect React Hook not work properly with dependency?
- how does promise and useState really work in React with AWS Amplify?
- How to use callback with useState hook in react
- How to prevent race conditions with react useState hook
- How to use React Context with useState hook to share state from different components?
- React hook useEffect() under the hood. Does an effect scheduled with useEffect block the main thread?
- how can I test if useState hook has been called with jest and react testing library?
- How to reset React hook state with setTimeout in useEffect
- How to check if state default boolean false is changing to true with React Hook useEffect
- How does React hook useRef() work under the hood? What is that reference exactly?
- How does this useState hook work in React?
- How to use useState hook in React with typescript correctly?
- UseEffect hook does not work or how to display async data
- React hook form does not work with input from reactstrap
- Why doesn't spread attributes work with React useState hook
- How do I make useState hook work with my function?
- How do I update 2 objects with React Hook useState
- How to do a custom logic and redirect to another pages in react or nextjs with useEffect hook
- Button 'more' in a popup box doesn’t work right with React hook useState
- What is inline-rendering in React router and how does it work with "render" inside a route/
- React hook useEffect failed to read new useState value that is updated with firebase's firestore realtime data
- How does useState hook know the calling context in react
- React hook useState with old state on children's first render useEffect
- Loading spinner with react useEffect hook and Redux and without useState hook
- how can i pass a useState hook defined with typescript down to another react component?
- React with TypeScript: how to type useState hook with previous state
- how to use react fetch() with useEffect hook and map the fetched data
More Query from same tag
- Dynamic importing a library next.js
- Simple mail send with fetch js and php
- ReactJS server side rendering fo single page application
- map.get() method isn't working with D3 v6
- how to hover svg and text both at a time
- Send address information to mailchimp using gatsby-plugin-mailchimp
- reactjs navlink activeClassName not working on root
- How to import image into React from public folder through a json file
- How to handle data fetch in react/redux component with propTypes?
- How do I correctly initialize a function which interacts with the redux state?
- Module not found: Can't resolve '../styles/global.css' in an next.js project
- ReactJS routing issue after deployment in production
- React - change state right after previous state change was rendered
- Gatsby.js blog is appending '/blog' twice to a post
- PHP, React, MySQL database contact form error
- How can i push array of strings in redux reducers?
- Applying "checked" attribute to radio group using React state. It assigns, but checked input isn't getting styles applied
- Insights into React Render LifeCycle
- TypeError: Object(...) is not a function in react redux
- How to correctly use Locomotive Scroll with Next.js routing?
- React app - ES6 external dependency
- Global beforeEach and afterEach Jest in React
- Version of useMemo for caching a value that will never change?
- How can I get list of all types from graphql schema?
- react components not talking to each other
- How to style React Native <CheckBox> component?
- How to condition the rendering of React function with hooks?
- Active object of the mapped array show center in scroll items react
- using multiple curly braces in React
- Why can't I run Webpack with React?