对于React/Redux构建中的数据流,我还是很困惑。当我在“我的应用程序”中工作时,我感觉相当舒适,但我无法弄清楚如何将数据从服务器传递到我的应用程序?React/Redux将道具传递给主要组件
我有反应-app.php文件与article#main-content
元素,这是我的应用程序的基本元素。我想传递一些产品的元素信息。
我该怎么做?
我想给支持productName,但我不知道如何从反应中访问它。
我的猜测是来自reducer ......但是如何?也许我不应该信赖article#main-content
并通过变量在$_GET
参数中作出反应?
反应-app.php
<link rel="stylesheet" type="text/css" href="/lines-configurator/build/style.css" media="screen"/>
<script async type="text/javascript" src="/lines-configurator/build/bundle.js"></script>
<article id="main-content" productName="My Product">
</article>
App.js:
import React from 'react';
import {render} from 'react-dom';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import reducer from './reducers/index';
import BoardConfig from './containers/BoardConfigContainer';
import BoardProduct from './containers/BoardProductContainer';
const store = createStore(reducer, window.devToolsExtension && window.devToolsExtension());
render(
<Provider store={store}>
<div>
<BoardConfig />
<BoardProduct />
</div>
</Provider>,
document.getElementById('main-content')
);
BoardProductContainer.js
import {connect} from 'react-redux';
import BoardProduct from '../components/BoardProductComponent';
export default connect(
function mapStateToProps(state) {
return {
};
},
function mapDispatchToProps(dispatch) {
return {};
}
)(BoardProduct);
BoardProductComponent.js
import React from 'react';
export default function() {
return();
}
个
BoardProductReducer.js
import * as types from '../constants/constants';
import Immutable from 'immutable';
const initialState = Immutable.Map({});
export default function boardReducer(state = initialState, action) {
switch (action.type) {
default:
return state;
}
}
我觉得不使用ajax请求非常糟糕,但我会使用全局窗口值。 :D非常感谢。 – Michal