我正在尝试通过单击按钮来调度一个动作。
我已经创建了我的组件。我将它连接到由 Provider 向下传递给组件的商店。但我收到一个错误:
未捕获的 TypeError:this.doSearchClick 不是函数
我有我的进口:
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import action_doSearch from '../../actions/searchActions'
我的组件:
class SearchForm extends React.Component {
constructor(props, doSearchClick) {
super(props);
this.search = this.search.bind(this);
this.doSearchClick = doSearchClick;
}
search() {
this.doSearchClick('bla bla from search');
}
render() {
return(
<div>
<button onClick={this.search}>Search</button>
</div>
);
}
}
不确定这是否需要:
SearchForm.propTypes = {
doSearchClick: PropTypes.func.isRequired
};
最后是connect
东西:
const mapStateToProps = (state) => {
return {
state
}
};
const mapDispatchToEvents = (dispatch) => {
return {
doSearchClick: (searchCriteria) => {
dispatch(action_doSearch(searchCriteria));
}
};
};
const SearchFormConnected = connect(
mapStateToProps,
mapDispatchToEvents
)(SearchForm);
在顶层,我通过 Provider 将商店向下传递:
import { Provider } from 'react-redux'
const finalCreateStore = compose(
applyMiddleware(
middleware,
thunk
),
DevTools.instrument()
)(createStore);
const store = finalCreateStore(reducer);
ReactDOM.render(
<Provider store={store}>
....
我还尝试通过上下文访问商店(不起作用并且可能已弃用)以及使用@connect
属性(给我一个类似的错误)来实现这一点。