給出了以下 React 組件:import React, { useEffect, useState } from "react";import { useDispatch, useSelector } from "react-redux";import { store, StoreState } from "../../redux/actions";import { setBackgroundAction } from "../../redux/title.actions";import "./Loader.scss";interface ReduxProps { bgClass: string;}interface Props extends ReduxProps { bgChange?: boolean;}export default function Loader(props: Props) { const [bgClassOld, setBgClassOld] = useState<string>(""); const dispatch = useDispatch(); useEffect(() => { const { bgChange, bgClass } = props; if (bgChange) { setBgClassOld(bgClass); dispatch(setBackgroundAction("bg-white")); dispatch(setBackgroundAction(bgClassOld)); } }); return ( <div className="d-flex"> <div className="loader"> <img src="/loadscreen.gif" /> </div> </div> );}// function mapping(state: StoreState): ReduxProps {// return {// bgClass: state.title.backgroundClass,// };// }這更像是一個理論上的問題,看看如何實際進行以下更改:該組件Loader將從另一個 npm 包(共享組件)導入。我的問題是我在當前實現中包含了一個 redux 狀態(將它從 Class 更改為 Functional 組件,所以mapping()它仍然在那里)。因為我只在我的“主”客戶端中導入組件,所以我不會有整個 redux 設置。所以我認為我需要通過store和傳遞dispatch函數props。那么我應該為我的組件創建一個道具store,當我導入共享組件時我會在其中傳遞 redux 存儲嗎?我是否還為每個調度函數創建兩個道具?是否有意義或會有更好的方法?
通過 props 傳遞 react-redux 存儲和調度功能?
MMMHUHU
2022-10-13 15:56:55