score:0
You should initialize the data each time the useEffect
is called inside useGet
:
function useGet<TData>(getData: () => Promise<TData>): TData | undefined {
const [data, setData] = React.useState<TData | undefined>(undefined);
React.useEffect(() => {
setData(undefinded) // initializing data to undefined
getData()
.then((fetched) => setData(fetched));
}, [getData]);
// (TODO later -- handle abort of data fetching)
return data;
}
score:2
In a reply on Twitter, @dan_abramov wrote that my useGet2
solution is more-or-less canonical:
If you do setState inside of render [and outside of
useEffect
] to get rid of stale state, it shouldn't ever produce a user-observable intermediate render. It will schedule another re-render synchronously. So your solution should be sufficient.It's the idiomatic solution for derived state, and in your example state is derived from ID.
How do I implement getDerivedStateFromProps?
(In longer term there will be a different solution for data fetching altogether that doesn’t involve effects or setting state. But I'm describing what we have today.)
The article referenced from that link -- You Probably Don't Need Derived State -- explains what the root cause of the problem is.
It says that the problem is, expecting the "controlled" state passed-in to User
(i.e. the userId) to match the "uncontrolled" internal state (i.e. the data returned by the effect).
The better thing to do is to depend on one or the other but not mix them.
So I suppose I should return a userId inside and/or with the data.
Source: stackoverflow.com
Related Query
- Avoid old data when using useEffect to fetch data
- React component not re-rendering on URL parameter change when using useEffect hook to fetch data
- React useEffect to fetch data not finished yet when try to sort data using React Router useSearchParams
- 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
- React Hook useEffect : fetch data using axios with async await .api calling continuous the same api
- What's the recommended place to fetch user data using Redux when app starts?
- Can I fetch data only once when using swr?
- Using redux, how to avoid rendering old state data before making a new API call?
- I can't fetch data from the api using useEffect
- How to Avoid Fetching Data Twice When Using React Hooks
- How do I avoid an infinite loop in useEffect when using Typescript?
- fetch data inside useEffect using action in redux
- Property 'then' does not exist on type 'RelayObservable<unknown>'. when I try to fetch data using relay in react. I don't know why this error come
- Trying to fetch axios data with redux, and load it in a component using useEffect
- (Reactjs) "delay" when fetching data using useEffect and useState
- I'm trying to fetch data from a server using useEffect react hook
- How do I avoid infinite loop when using Redux state in useEffect dependency array?
- How can I avoid to get too much recursion when storing data using firebase?
- React, using useEffect to updata data, a data is a Array, but when I use map or Object.keys(data).map, both are not work. How to make it work?
- Using React useEffect in a loop to fetch data from an API multiple times
- how to avoid undefined data when getting data from redux using useSelector?
- Receiving error when using setState within fetch to set response data and boolean value of state
- local storage problem when trying to fetch data from localstorage using useEffect() in ReactJS
- How do you avoid extra React component renderings when using useEffect hook?
- Invalid hook call when trying to fetch data using useCallback
- React error when trying to get data from api using useEffect hook
- Data is undefined in try catch block inside useEffect using async/await, but works when switching to Promises
- Its correct to not include a dependency on purpose on useEffect to avoid re-renderes when using react-route?
- How can I fetch data in getInitialProps in server side when using next.js with redux?
More Query from same tag
- How to break infinite loop in componentDidUpdate in reactjs
- React Passing Data to Stateful Component from Another Component
- React js - Add unique key for ant design switch
- How can we import index.js by default ,and reducer which is not inside index.js file
- React | Multiple Context | Not able to consume default values
- Material-UI Capitalize words in React
- What is the best way to CRUD children of a field in firebase cloud firestore?
- Why does 'yield' throw an error when used twice in saga?
- How to console.log() this function?
- Error: I18nextWithTranslation suspended while rendering, but no fallback UI was specified
- More than one getItem localStorage in a state
- Deleting Stateful element from ReactJS array
- React and Redux this.props.posts is undefined?
- using axios in react, how to specify a time limit for a get request to error out?
- cannot set State in functional component while fetching data from Firebase in reactJS
- Security of firebase realtime database rules
- How can I render out the items in my array one at a time when returning them?
- Call function inside constructor of component in react?
- Issue using React Hooks on an axios call
- JSON data printing issue in react-js
- How to properly using lodash throttle function in React?
- Why do we need a Single Page Application?
- Get onClickItem to Link to a URL or a route in react-simple-tree-menu
- API data rendering Issue in Child component in React
- Unable to delete the first value in input
- Customising label appearance RadioGroup in React
- React this.state.bottles.map is not a function
- Why can't this Spring Boot application find the main page?
- React Addup number During LongPress buttom?
- Hide and show number of characters in <p> - React