gpt4 book ai didi

reactjs - 如何在 React 或 React Native 上使用 useMutation 或 useQuery 作为钩子(Hook)?

转载 作者:行者123 更新时间:2023-12-04 16:42:14 24 4
gpt4 key购买 nike

那是我得到的异常(exception)......而且这对我来说没有意义......

无效的钩子(Hook)调用。钩子(Hook)只能在函数组件的主体内部调用。这可能是由于以下原因之一造成的:
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

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

最佳答案

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

You can’t use Hooks inside of a class component, but you can definitely mix classes and function components with Hooks in a single tree. Whether a component is a class or a function that uses Hooks is an implementation detail of that component. In the longer term, we expect Hooks to be the primary way people write React components.

关于reactjs - 如何在 React 或 React Native 上使用 useMutation 或 useQuery 作为钩子(Hook)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com