创建Redux中的store
#先安装redux
yarn add redux
创建store
在src下新建一个folder--store,然后在store下创建index.js
src/store/index.js
import { createStore } from 'redux';
import reducer from './reducer';
const store = createStore(reducer);
export default store;
src/store/reducer.js
const defaultStore = {
inputValue:'',
list:['apple','pear']
}
export default (state = defaultStore, action)=>{
return state;
}
src/TodoList.js
import React, { Component } from 'react';
import 'antd/dist/antd.css'
import store from './store' //store存储了公用的数据
import {
Input,
Button,
List
} from 'antd';
class TodoList extends Component{
constructor(props){
super(props)
// console.log(store.getState()); 拿到store所有数据
this.state = store.getState();
}
render(){
const {list} = this.state;
return (
<div style={{marginTop:"10px",marginLeft:'10px'}}>
<Input placeholder = 'todo info' style={{width:'300px',marginRight:'10px'}}/>
<Button type='primary'>提交</Button>
<List
bordered
dataSource={list}
renderItem={item => (
<List.Item>
{item}
</List.Item>
)}
/>
</div>
)
}
}
export default TodoList;