1.npm install redux --save
2.新建store文件夹,在该文件夹下新建Index.js
创建store
使用redux里的createStore方法
import { createStore } from 'redux';
import reducer from './reducer';
const store = createStore(reducer);
export default store;
把reducer传到store中
const defaultState = {
inputValue: '123',
list: [1, 2]
}
export default (state = defaultState, action) => {
return state;
}
**在相关项目文件中引入store
import store from './store';
constructor(props) {
super(props);
this.state = store.getState();
}
PS:建议利用react-redux管理redux,**标记步骤如下写
把provider放在最外层,传值store,则所有组件都可以拿到store中的数据
import { Provider } from 'react-redux';
import store from './store';
class App extends Component {
render() {
return (
<Provider store={store}>
<Header></Header>
</Provider>
);
}
}
关于react-redux详细使用续看文章
**react简书项目学习笔记26使用react-redux