【问题标题】:Where should I put my logic for writing new data to Firestore?我应该把将新数据写入 Firestore 的逻辑放在哪里?
【发布时间】:2021-09-18 02:56:19
【问题描述】:

我目前正在使用 React 和 Firebase 开展一个项目,目前我将新数据写入 Firestore 的逻辑直接在我的组件中。我现在也在集成 Redux,为此我正在使用 react-redux-firebase。我现在的问题是,把这个逻辑放在哪里最好,因为它让我的组件变得臃肿。

在react-redux-firebase的documentation中,他们也直接放在了组件里,但是他们的逻辑很简单,我的稍微复杂一点,比如这个handleSubmit:

const handleSubmit = async (e) => {
    e.preventDefault();
    setDisabled(true); //disable button to prevent multiple sbumits
    setLoading(true);

    try {
      const docRef = await firestore.collection("goodies").add(newGoodieData); // create new event
      await firestore.collection("goodies").doc(docRef.id).update({
        // add eventId to event doc
        id: docRef.id,
      });

      if (image) {
        const imageName = docRef.id + "." + image.name.split(".").pop(); //create image name with eventID and file extions

        const imageRef = storage.ref().child("goodie_images/").child(imageName); //create imageRef

        const imageSnapshot = await imageRef.put(image); //upload image to storage

        const downloadUrl = await imageSnapshot.ref.getDownloadURL(); //get image download-url

        const imagePath = imageRef.fullPath; // get image storage path

        await firestore.collection("goodies").doc(docRef.id).update({
          //update event with imageUrl and imagePath
          image: downloadUrl,
          imagePath: imagePath,
        });
      }
      setDisabled(false);
      setLoading(false);

      dispatch(showSuccessToast(`Created new goodie "${newGoodieData.name}"!`));
      history.push("/goodies");
    } catch (error) {
      dispatch(showErrorToast("Ops, an error occurred!"));
      console.error(error.message);
    }
  };

【问题讨论】:

    标签: reactjs firebase react-redux-firebase


    【解决方案1】:

    这完全是我的特质,但我使用如下结构。通常,组件和页面(即反应组件)名称是首字母大写:

    src
    |-blocks
    |-components
    |-css
    |-navigation
    |-pages
    |-services
    |-App.js
    |-constants.js
    |-index.js
    

    方块

    这些是我特定于应用程序的组件,以及一些更大的特定于应用程序的功能。

    |-blocks
      |-Block1
      | |-index.js
      | |-aSubModule.js
      |-Block2
      | |-index.js
      | |-aSubModule.js
    etc, etc
    

    组件

    这些是“通用”组件——即可以作为模块发布,或者从外部模块定制。这些不是特定于应用程序的。类似于“块”的结构

    |-components
      |-ComponentThatDoesThisThing
      |-ComponentThatDoesThatThing
    etc, etc
    

    CSS

    顾名思义……

    导航

    页眉、页脚、导航菜单。还有导航辅助功能(例如包装到页面的链接等)

    |-navigation
      |-Header
      |-Menu
      |-Footer
    etc, etc
    

    页数

    我所有的反应页面和子页面,无论需要什么深度。页面下方的所有内容都特定于应用程序

    |-pages
      |-Users
      | |-Home
      | | |index.js
      | | |-etc,etc
      | |-Account
      | |-Bio
      | |-etc,etc
      |-Owners
      | |-Home
      | |-Account
      | |-Bio
      | |-etc,etc
      |-index.js  
    

    您的问题在哪里得到更直接的解决:

    服务

    |-services
      |-reduxEnhancers
      |-reduxMiddleware
      |-reduxRootReducers
      |-slices
      |-configureStore.js
      |-firestore.js
    

    与教程没有太大区别。但这就是我的切片中的内容 - 或多或少对应于 redux 状态切片:

    切片

    |-services
      |-slices
        |-UserSlice
        | |-actions
        | | |-index.js
        | |-reducer
        | | |-index.js
        | |-business
        | | |-index.js
        | |-index.js
        |-OwnerSlice
        | |-actions
        | | |-index.js
        | |-reducer
        | | |-index.js
        | |-business
        | | |-index.js
        | |-index.js
        |-KitchCupboardSlice
        | |-actions
        | | |-index.js
        | |-reducer
        | | |-index.js
        | |-business
        | | |-index.js
        | |-index.js
        |-etc,etc slices
        |-index.js
    

    重要提示:大多数 Redux 教程显示 Redux 切片具有与 React 组件相同的分支结构。一般来说,这根本不是这种情况,而且我还没有找到它甚至有用的情况。你的数据的树状结构不一定像你的页面和反应组件的树状结构 - 按照 data 想要你的方式划分你的 Redux 存储,而不是用户的方式- 面对 React 想要你。本论坛更重要的是:让您的数据的 FIRESTORE 结构为您提供指导。

    使用中:

    |-services
      |-slices
        |-UserSlice
        |-index.js
    

    ...例如,从actionsreducerbusiness 导入然后导出所有公共/导出符号。

    |-services
      |-slices
        |-UserSlice
         |-reducer
    

    ...reducer 使用 store.injectReducer 将自己添加到 Redux 存储中,使用中定义的符号和常量

    |-services
      |-slices
        |-UserSlice
         |-actions
    

    ...actions,它还创建特定的 Redux 操作和帮助程序样板(操作、选择器等)。

    为了帮助区分责任:

    |-services
      |-slices
        |-UserSlice
         |-business
    

    ...business 子目录包含您提到的业务逻辑 - React 组件可能需要调用的任何逻辑,这些逻辑并非专门针对 React 模块的显示和状态方面。这也是我定义对 firestore 或任何其他外部服务或获取的任何访问的地方 - React 组件不应该关心这是如何完成的。

    为了更方便地访问所有这些不同的导出,我使用了

    |-services
      |-slices
        |-index
    

    ...从所有切片中导入然后导出所有公共/导出符号。可以更轻松地导入各种 React 组件,而无需担心 slices 的底层结构发生变化

    正如我所说,完全是我的特质,但它有助于区分责任。

    【讨论】:

    • 顺便说一句,我将我的大部分“切片”和 Firestore 与我的 Cloud Functions(不是 Redux,也许不是很明显)共享 - 操作/reducers/business 的另一个原因分配。 Cloud 函数中仅使用了 ```business` 部分(和 firestore)。为了实现这一点,我有一个 npm 包 @leaddreamer/firebase-wrappers,它以一种通用的方式包装了所有的 firebase/firestore 操作 - 请注意它的 FirebaseStorage 部分仍然是特定于我的,并且尚未对客户端 Firebase 存储进行完整的管理端模拟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2012-05-13
    • 2017-04-19
    • 2018-06-14
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多