Javascript This.props.dispatch 不是函数 - React-Redux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36850988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This.props.dispatch not a function - React-Redux
提问by richTheCreator
I am trying to dispatch an action from within my smart component. I've tried to use the mapDispatchToProps
and this.props.dispatch(actions.getApplications(1))
but neither is binding the actions to props
.
我正在尝试从我的智能组件中调度一个动作。我尝试使用mapDispatchToProps
并且this.props.dispatch(actions.getApplications(1))
都没有将操作绑定到props
。
Im not sure if it is because my mapStateToProps
is not included? I tried to include it but it did not work either.
我不确定mapStateToProps
是不是因为我的不包括在内?我试图包含它,但它也不起作用。
Any help is appreciated, I apologize for the length of the code block below.
任何帮助表示赞赏,我为下面的代码块的长度道歉。
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>
)}
}
回答by markerikson
Per the Redux FAQ question at here, this.props.dispatch
is available by defaultif you do notsupply your own mapDispatchToProps
function. If you dosupply a mapDispatchToProps
function, you are responsible for returning a prop named dispatch
yourself:
根据此处的 Redux FAQ 问题,如果您不提供自己的函数,this.props.dispatch
则默认情况下可用。如果你确实提供了一个函数,你有责任返回一个名为你自己的 prop :mapDispatchToProps
mapDispatchToProps
dispatch
const mapDispatchToProps = dispatch => ({
...other methods,
dispatch // ← Add this
})
export default connect(null, mapDispatchToProps)(Component)
Or, you can make sure your action creators are pre-bound using Redux's bindActionCreators
utility, and skip having to worry about using this.props.dispatch
in your component.
或者,您可以确保您的动作创建者使用 Redux 的bindActionCreators
实用程序预先绑定,而不必担心this.props.dispatch
在您的组件中使用。
回答by Interrobang
As you mentioned in your question, mapDispatchToProps
should be the secondargument to connect
.
当你在你的问题中提到,mapDispatchToProps
应该是第二个参数connect
。
If you don't have any state mapping to be done, you can pass null
as the first argument:
如果您没有要完成的任何状态映射,则可以将其null
作为第一个参数传递:
export default connect(null, mapDispatchToProps)(ApplicationContainer);
After you do this, then this.props.getApplications
will be bound.
在你这样做之后,然后this.props.getApplications
将被绑定。
As per your comment, if you want to have access to this.props.dispatch
INSTEAD of binding actions, simply call connect()
without passing any mappers, and the default behavior will inject dispatch
.
根据您的评论,如果您想访问this.props.dispatch
绑定操作的 INSTEAD,只需调用connect()
而不传递任何映射器,默认行为将注入dispatch
.
If you want to have BOTH bound action creators and this.props.dispatch
, you need to add a dispatch
to the object that you pass to mapDispatchToProps
as well. Something like dispatch: action => action
. Or, since you're not putting everything under a root key (like actions
), you could do:
如果您想要同时绑定动作创建者和this.props.dispatch
,您还需要将 a 添加dispatch
到您传递给的对象mapDispatchToProps
。类似的东西dispatch: action => action
。或者,由于您没有将所有内容都放在根键下(例如actions
),您可以执行以下操作:
function mapDispatchToProps(dispatch) {
let actions = bindActionCreators({ getApplications });
return { ...actions, dispatch };
}