reactjs 如何在useEffect中重用相同的功能,并在组件中重用另一个操作?

ds97pgxw  于 2023-02-03  发布在  React
关注(0)|答案(3)|浏览(117)
    • bounty将在6天后过期**。回答此问题可获得+50声望奖励。camille希望引起更多人关注此问题。

我想在一个组件中重用函数resolveSessionData()。它用于在属性更改和用户操作中设置一些状态。这是重用该功能的正确方法吗?或者在React中是否有更好的方法?

const { useEffect, useState } = require("react");

const Component = ({someProp}) => {
  const [itemData, setItemData] = useState(null);

  useEffect(() => {
    if (someProp) {
        resolveSessionData();
    }
  }, [someProp]);

  const resolveSessionData = () => {
    const data = database.getItemData();
    setItemData(data);
  };

  return <div>
    {/* show item data */}
    <button onClick={resolveSessionData}>
        Activate Lasers
    </button>
  </div>;
};
ma8fv8wu

ma8fv8wu1#

是的,这是正确的方法。在React中没有更好的方法。

s3fp2yjn

s3fp2yjn2#

您做得很好,但是如果您在resolveSessionData中引入了任何状态值,请使用useCallback版本(注解代码)

import React, { useState, useEffect, useCallback } from "react";

export default function App({ someProp }) {
  const [itemData, setItemData] = useState(null);

//   const resolveSessionData = useCallback((e) => {
//     e.preventDefault();
//    const data = database.getItemData();
//    setItemData(data);
//  },[])
  useEffect(() => {
    if (someProp) {
      resolveSessionData();
    }
  }, [someProp]);

  const resolveSessionData = (e) => {
     e.preventDefault();
    const data = database.getItemData();
    setItemData(data);
  };

  return (
    <div>
      {/* show item data */}
      <button onClick={resolveSessionData}>Activate Lasers</button>
    </div>
  );
}
ndh0cuux

ndh0cuux3#

为了重用useEffect和其他函数的功能,这里您可以使用自定义钩子,并在自定义钩子中覆盖公共功能,然后在需要的组件中重用**。
例如

import React, { useCallback, useEffect, useState } from "react";

// ----------------------------------  Custom Hook  -----------------------------

const useCustomFetch = ({someProp}) => {
    const [itemData, setItemData] = useState(null);

    const resolveSessionData = useCallback(() => {
        const data = database.getItemData(); // fetching data
        setItemData(data); // setting fetched data to state
    }, []);

    useEffect(() => {
        if (someProp) {
            resolveSessionData(); // or may be fetch data based on props
        }
    }, [someProp]);

    const addNewItem = useCallback(function (dataToAdd) {
        /**
         * write code to add new item to state
        */
    }, [])

    const removeExistingItem = useCallback(function (dataOrIndexToRemove) {
        /**
         * write code to remove existing item from state
        */
    }, [])

    return {
        itemData,
        addNewItem,
        removeExistingItem,
    }
}

// ----------------------------------  Component managing users  -----------------------------

const ComponentUser = ({someProp}) => {
  const {
    addNewItem,
    itemData,
    removeExistingItem
  } = useCustomFetch('users')

  const handleAction = useCallback(function (action, data) {
    if (action === 'add') {
        addNewItem(data) // ex. adding user
    } else if (action === 'remove') {
        removeExistingItem(data) // ex. removing user
    }
  }, [])

  return <div>
    {itemData.map(function () {
        /**
         * render items here // ex. listing users
         */
    })}
    <button onClick={handleAction}>
        Add / Remove
    </button>
  </div>;
};

// ----------------------------------  Component managing posts  -----------------------------

const ComponentPosts = ({someProp}) => {
    const {
      addNewItem,
      itemData,
      removeExistingItem
    } = useCustomFetch('posts') // re-using custom hook
  
    const handleAction = useCallback(function (action, data) {
      if (action === 'add') {
          addNewItem(data) // ex. add new post
      } else if (action === 'remove') {
          removeExistingItem(data) // ex. remove existing post
      }
    }, [])
  
    return <div>
      {itemData.map(function () {
          /**
           * render items here // ex. render posts
           */
      })}
      <button onClick={handleAction}>
          Activate Lasers
      </button>
    </div>;
};

相关问题