Javascript This.props.dispatch不是函数-React Redux
我正在尝试从智能组件中分派操作。我尝试过使用Javascript This.props.dispatch不是函数-React Redux,javascript,reactjs,ecmascript-6,redux,react-redux,Javascript,Reactjs,Ecmascript 6,Redux,React Redux,我正在尝试从智能组件中分派操作。我尝试过使用mapDispatchToProps和this.props.dispatch(actions.getApplications(1)),但都没有将操作绑定到props 我不确定是不是因为我的MapStateTops不包括在内?我试着把它包括进去,但也没用 非常感谢您的帮助,对于下面代码块的长度,我深表歉意 import classNames from 'classnames'; import SidebarMixin from 'global/jsx/s
mapDispatchToProps
和this.props.dispatch(actions.getApplications(1))
,但都没有将操作绑定到props
我不确定是不是因为我的MapStateTops
不包括在内?我试着把它包括进去,但也没用
非常感谢您的帮助,对于下面代码块的长度,我深表歉意
import classNames from 'classnames';
import SidebarMixin from 'global/jsx/sidebar_component';
import Header from 'common/header';
import Sidebar from 'common/sidebar';
import Footer from 'common/footer';
import AppCard from 'routes/components/appCard';
import { getApplications } from 'redux/actions/appActions';
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import actions from 'redux/actions';
import { VisibilityFilters } from 'redux/actions/actionTypes';
class ApplicationContainer extends React.Component {
constructor(props){
super(props)
this.state = {
applicants: []
}
this.onDbLoad = this.onDbLoad.bind(this)
}
onDbLoad(){
console.log(this.props.dispatch)
// this.props.getApplications(1)
}
render() {
return (
<Col sm={12} md={4} lg={4}>
<PanelContainer style={panelStyle}>
<Panel>
<PanelBody >
<Grid>
<Row>
{ this.onDbLoad() }
</Row>
</Grid>
</PanelBody>
</Panel>
</PanelContainer>
</Col>
)}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({ getApplications: getApplications },dispatch)
}
export default connect(null, mapDispatchToProps)(ApplicationContainer);
@SidebarMixin
export default class extends React.Component {
render() {
const app = ['Some text', 'More Text', 'Even More Text'];
var classes = classNames({
'container-open': this.props.open
})
return (
<Container id='container' className={classes}>
<Sidebar />
<Header />
<Container id='body'>
<Grid>
<Row>
<ApplicationContainer />
</Row>
</Grid>
</Container>
<Footer />
</Container>
)}
}
从“classNames”导入类名;
从“global/jsx/sidebar_component”导入SidebarMixin;
从“通用/标题”导入标题;
从“公共/侧边栏”导入侧边栏;
从“通用/页脚”导入页脚;
从“路由/组件/AppCard”导入AppCard;
从'redux/actions/appActions'导入{getApplications};
从“react redux”导入{connect}
从“redux”导入{bindActionCreators}
从“redux/actions”导入操作;
从'redux/actions/actionTypes'导入{VisibilityFilters};
类ApplicationContainer扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
申请人:[]
}
this.onDbLoad=this.onDbLoad.bind(this)
}
onDbLoad(){
console.log(this.props.dispatch)
//this.props.getApplications(1)
}
render(){
返回(
{this.onDbLoad()}
)}
}
功能图DispatchToprops(调度){
返回bindActionCreators({getApplications:getApplications},dispatch)
}
导出默认连接(null,mapDispatchToProps)(ApplicationContainer);
@侧杆菌混合素
导出默认类扩展React.Component{
render(){
const-app=[‘一些文本’、‘更多文本’、‘甚至更多文本’];
var classes=类名({
“容器打开”:this.props.open
})
返回(
)}
}
正如您在问题中提到的,mapDispatchToProps
应该是connect
的第二个参数
如果没有任何要进行的状态映射,可以将null
作为第一个参数传递:
export default connect(null, mapDispatchToProps)(ApplicationContainer);
执行此操作后,将绑定this.props.getApplications
根据您的评论,如果您想访问
this.props.dispatch
而不是绑定操作,只需调用connect()
,而不传递任何映射程序,默认行为将注入dispatch
如果您希望同时拥有绑定动作创建者和this.props.dispatch
,则还需要向传递给mapDispatchToProps
的对象添加dispatch
。类似于dispatch:action=>action
。或者,由于您没有将所有内容都放在根键下(如操作
),您可以执行以下操作:
function mapDispatchToProps(dispatch) {
let actions = bindActionCreators({ getApplications });
return { ...actions, dispatch };
}
根据Redux常见问题解答,如果您不提供自己的
mapDispatchToProps
功能,则此.props.dispatch可通过默认值使用。如果您确实提供了mapDispatchToProps
功能,您有责任自行返回名为dispatch
的道具:
const mapDispatchToProps=dispatch=>({
…其他方法,
调度//← 加上这个
})
导出默认连接(null,mapDispatchToProps)(组件)
或者,您可以确保您的动作创建者使用Redux的实用程序预先绑定,并且不用担心在组件中使用
this.props.dispatch
。感谢您的快速响应,我也尝试过您的解决方案,但dispatch仍然没有显示为道具
。您是否特别希望通用地使用dispatch
?或者你希望你的其他行动受到约束。如果是前者,我可以更新答案。我希望分派一个操作,以便它将通过还原器运行。因此,通常您将使用绑定操作,而不是手动调用this.props.dispatch
。是否要手动生成操作?是的,我要在组件渲染库上为响应生成操作。MymapDispatchToProps
返回bindActionCreators