Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

与umi 的 useRequest 一起使用时,捕获不到全局错误 #36

Open
lichia9527 opened this issue Jun 4, 2020 · 4 comments
Open

Comments

@lichia9527
Copy link

当和umi的useRequest一起使用时,我在组件最外层配置了UseAPIProvider的onError属性,当接口500时,不执行onError的回调函数

// useUser.js
const useUser = () => {
  const [userInfo, setUser] = useState();
  const { run, loading } = useRequest('/api/user', {
    manual: true
  });

  useEffect(() => {
    run().then(user => setUser(user));
  }, []);

  return { userInfo, loading, run };
};

export default createModel(useUser);
// App.js
import { UseAPIProvider } from '@umijs/use-request';
const App = () => {
return (
     <UseAPIProvider
        value={{
          onError: (e) => console.log(e)
        }}
    >
      <Children />
    </UseAPIProvider>
)
}
// Children.js

const Chidren = () => {
     const user = useUser()
     return <h1>Chidren</h1>
}

我直接使用useRequest是可以的捕获到500错误的

@awmleer
Copy link
Member

awmleer commented Jun 5, 2020

这个的确是预期行为,因为 hox 的状态是存在一个独立的组件树之中的,所以你在 App 组件中创建 UseAPIProvider 是不能捕获到报错的。
这个问题也许在 hox v2 中能得到解决,目前的临时方案是只能自己去处理报错

cc @brickspert

@h-a-n-a
Copy link
Contributor

h-a-n-a commented Jun 6, 2020

这个的确是预期行为,因为 hox 的状态是存在一个独立的组件树之中的,所以你在 App 组件中创建 UseAPIProvider 是不能捕获到报错的。
这个问题也许在 hox v2 中能得到解决,目前的临时方案是只能自己去处理报错

cc @brickspert

v2 这部分有什么解决思路吗?

@awmleer awmleer mentioned this issue Jun 8, 2020
@aweiu
Copy link

aweiu commented Aug 30, 2020

暂时可以这样:https://zhuanlan.zhihu.com/p/205453249

@X-neuron
Copy link

https://codesandbox.io/s/stupefied-euler-jgq93?file=/src/CountBtn.js
recoil 也能逻辑复用... 多一个atom

@awmleer awmleer mentioned this issue May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants