我剛開始在我的反應網站上使用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