Reactjs 将大型代码移动到单独的文件中,这是一个钩子吗?
我有一个react本机屏幕,它有一个很长的代码,我想对其进行折射 假设我的Reactjs 将大型代码移动到单独的文件中,这是一个钩子吗?,reactjs,react-native,react-hooks,Reactjs,React Native,React Hooks,我有一个react本机屏幕,它有一个很长的代码,我想对其进行折射 假设我的screen.jsx是(当然是简化的): 然后在我的Screen.jsx文件中,我从“../hooks/useRestaurantSearch”导入它importuserestaurantsearch和内部函数屏幕()我抓取我需要的常量 const [ initSearch, errorMessage, restaurants, setRestaurants, fetchingRestaurants,
screen.jsx
是(当然是简化的):
然后在我的Screen.jsx
文件中,我从“../hooks/useRestaurantSearch”导入它importuserestaurantsearch代码>和内部函数屏幕()
我抓取我需要的常量
const [
initSearch,
errorMessage,
restaurants,
setRestaurants,
fetchingRestaurants,
] = useRestaurantSearch();
这是可行的,但我觉得它可以写得更好,而且整个方法看起来很奇怪——它真的是一个定制的钩子吗?如果它不是自定义钩子,它是否作为实用程序属于util文件夹
你将如何处理这个问题
export default function useRestaurantSearch() {
// The code I mentioned goes here
return [initSearch, errorMessage, restaurants, setRestaurants, fetchingRestaurants];
}
const [
initSearch,
errorMessage,
restaurants,
setRestaurants,
fetchingRestaurants,
] = useRestaurantSearch();