反应本机阿波罗客户端 useQuery 无效钩子调用错误

时间:2021-02-25 15:01:57

标签: reactjs react-native graphql react-hooks apollo-client

如何在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);
    ;

}

1 个答案:

答案 0 :(得分:1)

它起作用了,谢谢

const [LoginUserMutation] = useMutation(LOGIN_USER_MUTATION);
const onLoginPressed = () => {
        LoginUserMutation({
            variables: {
                email: username.username,
                password: password.password
            }).then(data=>{
            console.log(data);
        }).catch(err=>{
            throw err;
        })
    }

}