reactjs - 具有使用 Hooks 的能力的 React Native Helper 功能组件
问题描述
我正在用 React Native 编写一个辅助组件。问题是它不允许我在其中使用钩子。
我见过一些人们建议使用类而不是功能组件的线程。但是 Classes 也不允许使用 Hooks。
这是我的代码:
import React, {useEffect, useState} from 'react';
import * as Constants from '../config/Constants';
import * as SecureStore from 'expo-secure-store';
import { useDispatch, useSelector } from 'react-redux';
import { setUser } from '../store/slices/userSlice';
import axios from 'axios';
// import { Container } from './styles';
const UserHelper = async () => {
// const { colors } = useTheme();
// const [user, setLocalUser] = useState();
const [loading, setLoading] = useState(true);
const [token, setToken] = useState();
const user = useSelector((state) => state.user);
// ======= my await function here which returns a dataset
const response = await axios.post(
"https://MY_ENDPOINT.execute-api.us-east-1.amazonaws.com/v1/",
API_GATEWAY_POST_PAYLOAD_TEMPLATE,
{ headers }
);
return response;
};
export default UserHelper;
那么如何定义一个 RN 助手类或功能组件,其中有几个函数可以使用钩子。
解决方案
我听取了@Francesco Clementi 的建议并创建了一个自定义钩子,它就像一个魅力。谢谢弗朗西斯科
推荐阅读
- stripe-payments - 如何将资金转移到非关联的 Stripe 账户
- flutter - Flutter:以毫米为单位创建标尺
- networking - 多播场景中的接收者如何知道它应该监听哪个 IP 地址?
- wordpress - 如何在不加载页面的情况下将简单的表单提交转换为 ajax 提交,例如将产品添加到购物车功能
- angular - 如何从角度的单个函数返回订阅和承诺?
- vb.net - VB.Net:Winforms Designer:在父自定义控件中使用自定义控件中的动词
- azure - 在 Azure 中通过 Powershell 执行远程命令,无需脚本
- c# - 需要解释一下,为什么EF核心现在需要使用attach
- airflow-scheduler - 当属性'catchup = false'时了解气流执行日期
- javascript - Angular 8 防止在重复选项卡上的浏览器选项卡之间共享会话