【发布时间】:2018-09-13 03:26:42
【问题描述】:
这样的做法是不好的吗?
SendInfoButton.js
import React from 'react';
import { sendInfo } from '../actions/index';
export const SendInfoButton = ({currentUser}) => (
<div>
<button onClick={() => sendInfo(currentUser)} />
</div>
)
actions/index.js
import { store } from '../reducers/index';
import { SEND_INFO } from '../constants/index;
export const sendInfo = (currentUser) => store.dispatch({type: SEND_INFO, payload: currentUser})
与使用 mapDispatchToProps 并将操作传递给不会使用它们的组件相比,以这种方式将操作直接导入组件似乎更有效。我也更倾向于导入这样的动作,因为我已经有了带有大量 props 的组件,不想再添加了。
【问题讨论】:
-
是的,这可能被认为是不好的做法,您正在硬编码应用程序以通过这种方式使用特定的
store实例,而不是通过道具获取商店dispatch。
标签: javascript reactjs ecmascript-6 redux