【发布时间】:2018-04-01 20:08:45
【问题描述】:
所以,我尝试了解 React/Redux 中的数据流是如何工作的,但我无法弄清楚如何在应用程序的嵌套组件中获取 store.getState(),例如在主 App 组件中。我通过Provider推送它,但是...
我做错了什么?
/* INDEX.JS */
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import TodoApp from './Reducers/TodoApp'
import App from './Components/App';
import './Styles/style.css';
const store = createStore(TodoApp);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
/* 嵌套组件 */
import React, { Fragment } from 'react';
import VisibleTodoList from '../Containers/VisibleTodoList';
import TodoFormAdd from '../Containers/TodoFormAdd';
class App extends React.Component {
constructor(props) {
super(props);
}
render() {
console.log(this.props.store.getState()); // this is not work - "undefined", why?
return(
<Fragment>
<TodoFormAdd />
<VisibleTodoList />
</Fragment>
);
}
}
export default App;
【问题讨论】:
-
我建议阅读
redux/react-redux教程,尤其是关于 reducers、actions 和connect的教程。它不像你描述的那样工作 -
谢谢。我做了,但是有很多白点。这就是为什么我写在这里是为了更深入地了解 Redux。 @TheReason
-
您将它传递给没有
的提供者。为此,您基本上必须使用 redux connect。
标签: reactjs redux react-redux