我正在处理react-redux示例:https://github.com/timscott/react-devise-sample
正在setup.js
中创建redux商店,如下所示:https://github.com/timscott/react-devise-sample/blob/master/client/src/app/setup.js
关键部分:
const initStore = ({onRehydrationComplete}) => {
store = createStore(
combineReducers({
...reactDeviseReducers,
cats: [],
form: formReducer,
router: routerReducer,
apollo: apolloClient.reducer()
}),
{},
compose(
applyMiddleware(
thunk,
routerMiddleware(history),
apolloClient.middleware()
),
autoRehydrate()
)
);
persistStore(store, {
blacklist: [
'form'
]
}, onRehydrationComplete);
return store;
};
在反应组件中的此文件之外:
CatsPage.js
我正在尝试.dispatch(loadCats()),以便反应应用程序将从服务器获取猫的列表,更新商店,并神奇地更新dom。这是我在CatsPage.js中的内容:
import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import CatList from './CatList';
import {loadCats} from '../../actions/catActions';
import {initStore} from '../../app/setup';
class CatsPage extends React.Component {
state = {
rehydrated: false
};
componentDidMount() {
initStore.dispatch(loadCats())
}
render() {
return (
<div>
<h1>Cats</h1>
<div>
<CatList cats={this.props.cats} />
</div>
</div>
);
}
}
CatsPage.propTypes = {
cats: PropTypes.array.isRequired
};
function mapStateToProps(state, ownProps) {
return {
cats: state.cats
};
}
export default connect(mapStateToProps)(CatsPage);
我收到错误:Uncaught TypeError: _setup.initStore.dispatch is not a function
我是react-redux初学者。
CatList.js
import React from 'react';
import PropTypes from 'prop-types';
import {Link} from 'react-router-dom';
const CatList = ({cats}) => {
return (
<ul className="list-group">
{cats.map(cat =>
<li className="list-group-item" key={cat.id}>
<Link to={'/cats/' + cat.id}>{cat.name}</Link>
</li>
)}
</ul>
);
};
CatList.propTypes = {
cats: PropTypes.array.isRequired
};
export default CatList;
答案 0 :(得分:1)
我假设您已正确设置了提供商。如果这样正确的方式来访问调度方法就是道具。所以你可以这样做。
this.props.dispatch(loadCats())