简体   繁体   English

如何重用一个组件确实在多个组件之间挂载了等效的钩子

[英]How to reuse a component did mount equivalent hook across multiple components

I have a useEffect hook that performs a few operations when the component mounts, this useEffect is repeated across many components and I'm wondering can I hoist these operations to a custom hook and then use that in each component, and what would that look like?我有一个useEffect挂钩,它在组件安装时执行一些操作,这个 useEffect 在许多组件中重复,我想知道我可以将这些操作提升到自定义挂钩,然后在每个组件中使用它,那会是什么样子?

My useEffect hook looks like:我的 useEffect 钩子看起来像:

useEffect(() => {
  performOperationOne();
  performOperationTwo();
}, []);

Do I simply create a hook usePerformOperation and add this useEffect logic then import the hook into each component and add the hook call into another:我是否只需创建一个钩子usePerformOperation并添加此useEffect逻辑,然后将钩子导入每个组件并将钩子调用添加到另一个组件中:

const triggerOperations = usePerformOperation();
useEffect(() => {
  triggerOperations()
}, []);

Is that correct?那是对的吗? Sorry still fuzzy on hooks.对不起,仍然模糊不清。

Update: Something like this:更新:像这样的东西:

import React from "react";
import usePerformOperation from "./usePerformOperation";

const Test = () => {
  usePerformOperation();

  return <h1>Testing Operations...</h1>;
};

export default Test;

It is situational, not every time you need to return something from the custom hooks.这是情境性的,并非每次您需要从自定义挂钩中返回某些内容时。

Consider custom hook as pice of code, that will be injected to your component.将自定义钩子视为代码的一部分,它将被注入到您的组件中。

In this case, you do not need anything to be returned from your hook.在这种情况下,你不需要从你的钩子返回任何东西。

function usePerformOperation () {
  useEffect(() => {
    performOperationOne();
    performOperationTwo();
  }, []);
 // nothing is returned
}

const Test = () => {
  usePerformOperation();

  return <h1>Testing Operations...</h1>;
};

is equivalent to相当于

const Test = () => {
  useEffect(() => {
    performOperationOne();
    performOperationTwo();
  }, []);
  return <h1>Testing Operations...</h1>;
};
import { useEffect, useState } from "react";
import axios from "axios";

export default () => {
    const [results, setResults] = useState([]);
    const [errorMsg, setErrorMsg] = useState("");

    //Method that calls API with parameter. You have to call this method to run the effect
    const searchAPI = async (_p) => {
    try {
      const res = await axios.get("/search", {
        params: {
          term: _p,
          limit: 40,
          location: "san jose",
        },
      });
      setErrorMsg("");
      setResults(res.data.businesses);
    } catch (ex) {
      setErrorMsg("Something went wrong");
    }
  };

  useEffect(() => {
    searchAPI("");
  }, []);

  return [searchAPI, results, errorMsg];
};

//Save above code in file name as useCustomHook //In your component file, import this hook and in your functional component, write //将上面的代码保存在文件名中为useCustomHook //在你的组件文件中,导入这个钩子,在你的功能组件中,写

const [searchAPI, results, errorMsg] = useCustomHook(); const [searchAPI, results, errorMsg] = useCustomHook();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM