我使用 Redux 和 Redux Saga 进行身份验证。我想测试它,如果我点击我的按钮(身份验证按钮),那么它应该重定向到主页。但我收到此错误:
无效的钩子调用
为什么?
import { all, take, call, put, fork } from 'redux-saga/effects';
import { authProccess, authFailure, authSuccess } from '../slice/auth';
import authApi from '../../../api/auth';
import { useNavigation } from '@react-navigation/native';
function* register(payload) {
try {
const navigation = useNavigation();
const res = yield authApi(payload).then(res => res).catch(e => e);
yield navigation.navigate('Home');
} catch(e) {
console.log(e);
}
}
function* watcher() {
while(true) {
const { payload } = yield take(authProccess.type);
yield fork(register, payload);
}
}
export default function* () {
yield all([watcher()]);
}
此代码工作正常,直到我添加 use Navigation 和 navigation.navigate
答案 0 :(得分:0)
hooks 只能在函数组件内部使用,
我会怎么做,我会在 reducer 中更新为 auth: true,如果 auth 为 true,则从屏幕导航。