【问题标题】:how to use useMutation or useQuery as hook on React or React Native?如何使用 useMutation 或 useQuery 作为 React 或 React Native 的钩子?
【发布时间】:2023-04-10 15:53:02
【问题描述】:

这是我得到的例外..这对我来说没有意义...

无效的钩子调用。 Hooks 只能在函数组件的主体内部调用。这可能由于以下原因之一而发生: 1. React 和渲染器的版本可能不匹配(例如 React DOM) 2. 你可能违反了 Hooks 规则 3. 你可能在同一个应用程序中拥有多个 React 副本 有关如何调试和修复此问题的提示,请参阅 fb.me/react-invalid-hook-call。

这是我的组件...我正在尝试仅归档标准登录/密码屏幕...

import React from 'react'
import { View, Text, TouchableWithoutFeedback, TextInput } from 'react- 
native'
import style from './style'
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome'
import { faUser, faUnlockAlt } from '@fortawesome/free-solid-svg-icons'
import { Query, useMutation } from 'react-apollo'
import { testQuery, LOGIN_MUTATION } from '../../gql/session'

class LoginForm extends React.Component {
constructor(props) {
    super(props)
    this.state = {
        inProgress: false,
        email: 'hello@hello.com',
        password: '1234'
    }
}
doLogin() {
    const [_doLogin ] = useMutation(LOGIN_MUTATION, {
        update: (proxy, mutationResult) => {
            console.log(mutationResult)
        }
    ,
    variables: {
      $email: this.setState.email,
      $password: this.state.password
    }
    })
     _doLogin()
}
render() {
    return (
        <View style={style.form}>
            <Text style={style.formLabel}>E-Mail</Text>
            <View style={style.formRow}>
                <FontAwesomeIcon size={28} style={style.formIcon} icon={faUser} />
                <TextInput
                    onChangeText={(email) => this.setState({ email })}
                    value={this.state.email}
                    keyboardType={'email-address'}
                    style={style.textInput} />
            </View>
            <Text style={style.formLabel}>Password</Text>
            <View style={style.formRow}>
                <FontAwesomeIcon size={28} style={style.formIcon} icon={faUnlockAlt} />
                <TextInput
                    onChangeText={(password) => this.setState({ password })}
                    value={this.state.password}
                    secureTextEntry={true}
                    style={style.textInput} />
            </View>

            <TouchableWithoutFeedback onPress={() => { this.doLogin() }}>
                <View style={[style.button, style.doLoginButton]}>
                    <Text style={style.buttonText}>Login</Text>
                </View>
            </TouchableWithoutFeedback>
            <View style={style.bellowButton}>
                <TouchableWithoutFeedback onPress={() => this.props.onCancel()}>
                    <Text style={style.cancel}>Cancel</Text>
                </TouchableWithoutFeedback>
                <TouchableWithoutFeedback onPress={() => this.props.onForgot()}>
                    <Text style={style.forgot}>Forgot ?</Text>
                </TouchableWithoutFeedback>
            </View>
        </View>
    )
   }
}


export default LoginForm

那么,怎么了?以及如何存档?

【问题讨论】:

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


    【解决方案1】:

    你得到的错误是因为你试图在类组件中使用钩子,所以在docs中他们提到了以下内容:

    你不能在类组件中使用 Hooks,但是你可以 绝对将类和函数组件与 Hooks 混合在一起 树。组件是使用 Hooks 的类还是函数 该组件的实现细节。从长远来看,我们 希望 Hooks 成为人们编写 React 组件的主要方式。

    【讨论】:

      【解决方案2】:

      来自错误给你的 fb.me/react-invalid-hook-call 链接:

      钩子只能在函数组件内部调用。

      你有一个类组件,你需要把它转换成函数式组件才能使用反应钩子。

      或者如果您习惯于对组件进行分类,请使用来自'@apollo/react-components'Mutation 组件。

      在此链接上查看更多信息:https://www.apollographql.com/docs/react/api/react-components/

      【讨论】:

        【解决方案3】:

        我想指出您当前使用的是class component,但挂钩设计用于functional component。因此,一旦您将组件更改为功能组件,您就可以按照如下所示的 apollo graphql hooks 官方文档中的示例进行操作,然后针对您的用例进行尝试。

        import gql from 'graphql-tag';
        import { useMutation } from '@apollo/react-hooks';
        
        const ADD_TODO = gql`
          mutation AddTodo($type: String!) {
            addTodo(type: $type) {
              id
              type
            }
          }
        `;
        
        function AddTodo() {
          let input;
          const [addTodo] = useMutation(ADD_TODO);
        
          return (
            <div>
              <form
                onSubmit={e => {
                  e.preventDefault();
                  addTodo({ variables: { type: input.value } });
                  input.value = '';
                }}
              >
                <input
                  ref={node => {
                    input = node;
                  }}
                />
                <button type="submit">Add Todo</button>
              </form>
            </div>
          );
        }
        
        export default AddTodo();
        

        来源:https://www.apollographql.com/docs/react/essentials/mutations/

        【讨论】:

          【解决方案4】:

          试试这个 don froget instll package @apollo/react-hooks

          import React from 'react'
          import {View, Text, TouchableWithoutFeedback, TextInput} from 'react-
           native '
           import {useMutation} from '@apollo/react-hooks';
          
          import style from './style'
          import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
          import {faUser, faUnlockAlt} from '@fortawesome/free-solid-svg-icons'
          import {testQuery, LOGIN_MUTATION} from '../../gql/session'
          
          class LoginForm extends React.Component {
              constructor(props) {
                  super(props)
                  this.state = {
                      inProgress: false,
                      email: 'hello@hello.com',
                      password: '1234'
                  }
              }
          
              render() {
                  const [addTodo] = useMutation(ADD_TODO);
          
                  return (
                      <View style={style.form}>
                          <Text style={style.formLabel}>E-Mail</Text>
                          <View style={style.formRow}>
                              <FontAwesomeIcon size={28} style={style.formIcon} icon={faUser}/>
                              <TextInput
                                  onChangeText={(email) => this.setState({email})}
                                  value={this.state.email}
                                  keyboardType={'email-address'}
                                  style={style.textInput}/>
                          </View>
                          <Text style={style.formLabel}>Password</Text>
                          <View style={style.formRow}>
                              <FontAwesomeIcon size={28} style={style.formIcon} icon={faUnlockAlt}/>
                              <TextInput
                                  onChangeText={(password) => this.setState({password})}
                                  value={this.state.password}
                                  secureTextEntry={true}
                                  style={style.textInput}/>
                          </View>
          
                          <TouchableWithoutFeedback
                              onPress={() => {
                                ADD_TODO({
                                  variables: {
                                      email: this.setState.email,
                                      password: this.state.password
                                  },
                                  update: (proxy, mutationResult) => {
                                      console.log(mutationResult)
                                  }})}}>
          
          
                              <View
                              style={[style.button, style.doLoginButton]}>
                              <Text style={style.buttonText}>Login</Text>
                          </View>
                      </TouchableWithoutFeedback>
                      <View style={style.bellowButton}>
                          <TouchableWithoutFeedback onPress={() => this.props.onCancel()}>
                              <Text style={style.cancel}>Cancel</Text>
                          </TouchableWithoutFeedback>
                          <TouchableWithoutFeedback onPress={() => this.props.onForgot()}>
                              <Text style={style.forgot}>Forgot ?</Text>
                          </TouchableWithoutFeedback>
                      </View>
                  </View>
                  ) } }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-04-27
            • 2022-10-13
            • 1970-01-01
            • 2022-06-16
            • 2019-09-14
            • 2021-03-02
            • 2020-11-08
            • 2021-09-17
            相关资源
            最近更新 更多