【问题标题】:react native apollo client useQuery invalid hook call errorreact native apollo client useQuery invalid hook call error
【发布时间】: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


    【解决方案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;
            })
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 2021-06-04
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2020-04-13
      • 2022-11-24
      • 2021-07-06
      • 2020-10-07
      相关资源
      最近更新 更多