[英]ReactJs: get data from redux-store
I create the component, connect it to redux, initialized the state and created reducer.我创建组件,将其连接到 redux,初始化 state 并创建减速器。
Component:零件:
import fetchReducer from './reducers/fetchReducer';
import initialState from './constants/initialState';
const store = createStore(fetchReducer, initialState, devToolsEnhancer());
class Table extends React.Component {
// constructor
getDataFromState = function () {
let propsContent = this.props;
let itemsStored = propsContent.itemsProp;
return itemsStored;
};
getOperationItems = function () {
let itemsStored = this.getDataFromState();
itemsStored.map((item, index) => (
<li> key={index} item={item}</li>
))};
render() {
return (
<div className={styles}>
{this.getOperationItems()}
</div>
);
}
}
const mapStateToProps = (store) => {
return {itemsProp: store.items}
};
export default connect(mapStateToProps)(Table)
ReactDOM.render(
<Provider store={store}>
<Table/>,
</Provider>,
document.getElementById("app")
);
Initial state:初始 state:
export default {
items: [
{
'Date': 1,
'Operation': 1
}
]
}
Reducer:减速器:
import initialState from '../constants/initialState';
export default function update(state = initialState) {
return state;
}
But I cant get the initial data from store.但我无法从商店获取初始数据。 Debugging, I see in method Table#getDataFromState that propsContent contains the empty ({}) array - not the expected [{'Date': 1,'Operation': 1 }].调试时,我在 Table#getDataFromState 方法中看到 propsContent 包含空 ({}) 数组 - 而不是预期的 [{'Date': 1,'Operation': 1 }]。
Debugging also I see, that reducer receive the initial state with properly data: [{'Date': 1,'Operation': 1}]调试我也看到了,reducer 收到初始 state 和正确的数据:[{'Date': 1,'Operation': 1}]
How do you register the reducer passed to createStore?你如何注册传递给createStore的reducer? It is important in order to use it into mapStateToProps:在 mapStateToProps 中使用它很重要:
You can look here an example on how to register a reducer您可以在此处查看有关如何注册减速器的示例
UPDATES更新
The problem is how you are using the connected component Table .问题是您如何使用连接的组件Table 。 If you define it in another file and then import it, your code works perfectly, as you can see here如果您在另一个文件中定义它然后导入它,您的代码可以完美运行,如您在此处看到的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.