【发布时间】:2021-05-27 23:08:15
【问题描述】:
我们如何在 dispatch 函数中使用 appollo/client useQuery?当我尝试它时,我得到了这个错误 有些人认为我找到了,但我无法修复。 我们如何在 react 或 react native 中使用 apollo 客户端挂钩事件库? react native apollo client useQuery invalid hook call error
Error: Invalid hook call. Hooks can only be called inside of the body of a
function component. This
could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as
React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb .me/react-invalid-hook-call for tips about how to debug and
fix this problem.
- node_modules\react-native\Libraries\LogBox\LogBox.js:148:8 in registerError
.
.
.
actions.js
import { useQuery, gql } from '@apollo/client';
const LOGIN_USER_QUERY = gql`
query{
loginUser(email:$String, password:$String){
userId
token
expiredIn
}
}
`
export const loginUser = (email, password)=>dispatch=> {
const { loading, error, data } = useQuery( LOGIN_USER_QUERY, {
variables:{
email,
password
}
}).then(
data=>{
dispatch({
type: $AT.LOGIN_USER,
payload: data.loginUser
})
}
).catch(err=>{
throw err;
})
if(!data) throw "You couldn't login, Please try again.";
console.log(data);
;
}
【问题讨论】:
标签: reactjs react-native graphql react-hooks apollo-client