我刚开始在我的反应网站上使用redux。很小。 (并且我正在按照这个教程https://medium.com/front-end-developers/handcrafting-an-isomorphic-redux-application-with-love-40ada4468af4#.mhkgga84t插入一些代码到我的反应网站以防万一)。但是当我正在进行并尝试接受this.props.store
时,即使在我的应用程序中有<Provider store={store}>
也不可用。这里是我的客户端和应用程序代码(我可以提供更多的我不知道还有什么补充):react-redux react-router存储不可用于道具
// src/client.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router';
import { routes } from './routes';
import { browserHistory } from 'react-router'
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import * as reducers from './reducers';
import { fromJS } from 'immutable';
let initialState = window.__INITIAL_STATE__;
Object.keys(initialState).forEach(key => {
initialState[key] = fromJS(initialState[key]);
});
const reducer = combineReducers(reducers);
const store = createStore(reducer, initialState);
console.log('store', store);
ReactDOM.render(<Provider store={store}><Router routes={routes} history={browserHistory} store={store} /></Provider>, document.getElementById('app'));
,但我无法从this.props
访问store
。它应该在这里对吗?所以我最终可以做const { todos, dispatch } = this.props;
?
// src/components/app.js
import React from 'react';
import { Link } from 'react-router';
import HeaderComponent from './common/header';
import ShareFooterComponent from './common/share-footer';
import FooterComponent from './common/footer';
import { bindActionCreators } from 'redux';
import * as ListingActions from '../actions/listing';
import { connect } from 'react-redux';
export default class AppComponent extends React.Component {
render() {
console.log('apps', this.props);
return (
<div>
<HeaderComponent />
{ this.props.children }
<ShareFooterComponent />
<FooterComponent />
</div>
);
}
}
我是否缺少任何东西?
我试着在之前添加这段代码,但是在'@ connect',特别是'@'符号中出现了'意外错误'。然后我试着遵循这个http://redux.js.org/docs/api/bindActionCreators.html#-somecomponent-js,并在SomeComponent.js中读到它说'let {dispatch} = this.props //注入react-redux:'但没有出现。 – index
你的构建可能没有设置装饰器,即'@ func'。我想你会需要babel预设阶段0。您可以正常调用connect函数,如果您想阅读文档,那么它就是react-redux库的一个功能。你的用例应该看起来像'export default connect(state =>({todos:state.todos}))(AppComponent)' – patnz
好吧。当我在底部添加“AppComponent.contextTypes = {store:React.PropTypes.object}”时,我找到了'store',但它在'this.context'中。我应该使用那个还是尝试'connect'? – index