我的問題是,我把上面提到的方法放在哪里?因為在我想在其中使用 Redux 存盤的每個組件中,我需要基本上重復以下咒語,
import { useSelector, useDispatch } from "react-redux";
import { bindActionCreators } from "redux";
import * as actions from "../../redux/actions";
然后,例如,
const dispatch = useDispatch();
const { fetchStats } = bindActionCreators(actions, dispatch);
我看到有些人像containers檔案夾一樣制作?
另外,你的檔案結構是什么?你們把動作放在哪里?你如何匯出它們?全部在一個檔案中還是什么?在較大的專案中,它的效率并不高。
與往常一樣,提前致謝!
uj5u.com熱心網友回復:
答案是,不要。
bindActionCreators實際上只在 React-Reduxconnect函式內部使用過,應用程式代碼通常不使用。
今天,使用 React-Redux hooks API,我們建議只手動撰寫:
const dispatch = useDispatch();
const handleClick = () => dispatch(someActionCreator())
這樣,組件中實際發生的事情就更明確了。
是的,這確實需要將鉤子匯入到組件中。像任何其他功能一樣,使用它們既是有意的,也是必要的。
我們建議不要嘗試分離出“容器組件”,尤其是在您使用 hooks API 時。
至于邏輯,您應該使用 Redux Toolkit使用“功能檔案夾”檔案結構為每個包含邏輯的功能創建一個“切片”檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/369631.html
標籤:反应 还原 反应还原 redux-thunk
