Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 将大型代码移动到单独的文件中,这是一个钩子吗?_Reactjs_React Native_React Hooks - Fatal编程技术网

Reactjs 将大型代码移动到单独的文件中,这是一个钩子吗?

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,

我有一个react本机屏幕,它有一个很长的代码,我想对其进行折射

假设我的
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();