Comments (9)
Facing same issue, but with manual: false is giving data
from hooks.
@miaojinsing @mmarifat 这是一个我做测试的 demo,并没有出现 data 无法返回的情况
from hooks.
Hello @miaojinsing. Please provide a online reproduction by forking this link https://codesandbox.io/s/ok2fe or a minimal GitHub repository. Issues labeled by Need Reproduce
will be closed if no activities in 3 days.
你好 @miaojinsing, 我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击 此处 创建一个 codesandbox 或者提供一个最小化的 GitHub 仓库。3 天内未跟进此 issue 将会被自动关闭。
from hooks.
@miaojinsing @mmarifat 这是一个我做测试的 demo,并没有出现 data 无法返回的情况
我用这种方法防止重复请求 ,但 在react 开启 严格模式后 返回data数据是不会刷新的 一直是undefined , 网络返回数据是正常的
Hello @miaojinsing. Please provide a online reproduction by forking this link https://codesandbox.io/s/ok2fe or a minimal GitHub repository. Issues labeled by
Need Reproduce
will be closed if no activities in 3 days.你好 @miaojinsing, 我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击 此处 创建一个 codesandbox 或者提供一个最小化的 GitHub 仓库。3 天内未跟进此 issue 将会被自动关闭。
const init = useRef()
const { data , run } = useRequest(queryAttachmentGroupWithTotalAll,{ manual:true })
useEffect(()=>{
if(!init.current) {
init.current = true
run({ type:1 })
}
},[])
console.log(data)
from hooks.
Hello @miaojinsing. Please provide a online reproduction by forking this link https://codesandbox.io/s/ok2fe or a minimal GitHub repository. Issues labeled by
Need Reproduce
will be closed if no activities in 3 days.你好 @miaojinsing, 我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击 此处 创建一个 codesandbox 或者提供一个最小化的 GitHub 仓库。3 天内未跟进此 issue 将会被自动关闭。
from hooks.
In strict mode, useEffect is executed twice. After the first execution, init.current is true. When useEffect is executed for the second time, because init.current is true, the run method will not be executed, so the data is undefined.
If you want to execute the run method successfully, you can use async/await
https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22[https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22](url)
from hooks.
@miaojinsing @mmarifat 这是一个我做测试的 demo,并没有出现 data 无法返回的情况
我用这种方法防止重复请求 ,但 在react 开启 严格模式后 返回data数据是不会刷新的 一直是undefined , 网络返回数据是正常的
Hello @miaojinsing. Please provide a online reproduction by forking this link https://codesandbox.io/s/ok2fe or a minimal GitHub repository. Issues labeled by
Need Reproduce
will be closed if no activities in 3 days.
你好 @miaojinsing, 我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击 此处 创建一个 codesandbox 或者提供一个最小化的 GitHub 仓库。3 天内未跟进此 issue 将会被自动关闭。
const init = useRef() const { data , run } = useRequest(queryAttachmentGroupWithTotalAll,{ manual:true })
useEffect(()=>{ if(!init.current) { init.current = true run({ type:1 }) } },[])
console.log(data)
因为 useEffect 在 react 18 dev 模式 + 严格模式 useEffect 会挂载两次,这个是防止开发者在 dev 环境写出 bug,所以正确的写法是 useEffect 卸载的时候对 init 进行还原。增加一个 init.current = false,见下面的代码,即可正常拿到 data
更多请看下最新的官方文档:https://zh-hans.react.dev/learn/synchronizing-with-effects#fetching-data
const init = useRef()
const { data , run } = useRequest(queryAttachmentGroupWithTotalAll,{ manual:true })
useEffect(()=>{
if(!init.current) {
init.current = true
run({ type:1 })
return () => {
init.current = false;
}
}
},[])
console.log(data)
from hooks.
In strict mode, useEffect is executed twice. After the first execution, init.current is true. When useEffect is executed for the second time, because init.current is true, the run method will not be executed, so the data is undefined. If you want to execute the run method successfully, you can use async/await [https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22](https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22%5Bhttps://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22%5D(url))
I don't think this is a suitable solution. It only avoids the problem but does not solve it. It is recommended to refer to the above answer to solve this problem.
from hooks.
In strict mode, useEffect is executed twice. After the first execution, init.current is true. When useEffect is executed for the second time, because init.current is true, the run method will not be executed, so the data is undefined. If you want to execute the run method successfully, you can use async/await [https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22](https://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22%5Bhttps://codesandbox.io/p/sandbox/default-usage-forked-7tzrt5?file=%2FApp.tsx%3A21%2C10-21%2C22%5D(url))
I don't think this is a suitable solution. It only avoids the problem but does not solve it. It is recommended to refer to the above answer to solve this problem.
合情合理
from hooks.
Related Issues (20)
- useAntdTable 初始化请求执行2次 HOT 2
- useRequest在严格模式StrictMode下,缓存失效 HOT 9
- useInViewport 弄一個triggerOnce的參數比較好 HOT 3
- [RFC] useEventBus HOT 7
- 能不能把useTimeout升级一下? HOT 9
- 官网白屏 HOT 1
- [RFC]useSwipeEvent
- 官网国内镜像打不开了 HOT 2
- runAsync 时能否重新指定options
- useLongPress在pc电脑外接触控评时,不触发
- useUrlState doesn't delete initialState when state is set to undefined
- Virtual List did not Load New Data Rows After Scroll HOT 1
- resetState的随机数问题
- useAntdTable在manual=true的情况下,defaultParam和手动run都不会填充表单默认值 HOT 4
- [bug] useResponsive 源码缺乏 default 导出,与 babel-plugin-import 配合时出错
- useIsomorphicLayoutEffect更名 HOT 1
- 版本3.8.0 useSelections 可以增加兜底 HOT 1
- 请求添加@types HOT 1
- [RFC] useIsClient HOT 1
- useInterval重启有问题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hooks.