我想调用register函数onSubmit,但是当我试图控制它时,它显示undefined。我也试着像这样使用register
,它仍然是undefined。
Auth.js
const AuthProvider = ({children}) => {
const [signUp, setSignUp] = useState([]);
const [formData, setFormData] = useState(FORM_FIELDS);
const authState = {
signUpData: signUp,
setSignUp: setSignUp,
setFormData: setFormData,
formData: formData,
register: handleRegister,
};
const handleRegister = async () => {
const response = await axios
.post(process.env.API_BASE_URL + API_ENDPOINTS.SIGN_UP, {
email: formData.email,
first_name: formData.firstName,
last_name: formData.lastName,
password: formData.password,
})
.then(data => console.log(data, 'apiData'));
console.log(response, 'response');
};
return (
<ApiContext.Provider value={authState}>{children}</ApiContext.Provider>
);
};
export const ApiContext = createContext(AuthProvider);
export default AuthProvider;
Register.js
export default function MultiStepSignUp() {
const auth = useAuth();
const onSubmit = () => {
console.log(auth.register, 'auth');
// here console shows undefined
};
useAuth.js
import {useContext} from 'react';
import {ApiContext} from 'src/context/ApiContext';
export const useAuth = () => useContext(ApiContext);
1条答案
按热度按时间t2a7ltrp1#
以下是详细的答案和简化的答案。
这是IntroContext.js
如何在App.js文件中使用
这里你可以在LoginScreen.js中使用它
如果你有任何困惑,请在评论中询问。