# 1.绑定方法
class TodoList extends Component {
constructor(props, context) {
super(props, context);
this.state = store.getState();
this.changeInputValue=this.changeInputValue.bind(this);
this.clickButton=this.clickButton.bind(this);
this.storeChange=this.storeChange.bind(this);
store.subscribe(this.storeChange)
}
<Input
placeholder={this.state.inputValue}
value={this.state.inputValue}
style={{ width:'250px',marginRight:'10px',marginLeft:'10px'}}
onChange={this.changeInputValue}
/>
changeInputValue(e){
const action = {
type:'changeInput',
value:e.target.value
};
store.dispatch(action);
this.setState(store.getState())
}
# 2.Redux改变状态
if (action.type==='changeInput')
{
let newState = JSON.parse(JSON.stringify(state));
newState.inputValue=action.value;
return newState
}
# 3.完整代码
//src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import TodoList from './TodoList';
ReactDOM.render(<TodoList />, document.getElementById('root'));
//src/TodoList.js
import React, {Component} from 'react';
import 'antd/dist/antd.css'
import {Input , Button ,List} from "antd";
import store from "./store";
class TodoList extends Component {
constructor(props, context) {
super(props, context);
this.state = store.getState();
this.changeInputValue=this.changeInputValue.bind(this);
this.clickButton=this.clickButton.bind(this);
this.storeChange=this.storeChange.bind(this);
store.subscribe(this.storeChange)
}
render() {
return (
<div style={{margin:'10px'}}>
<div>
<Input
placeholder={this.state.inputValue}
value={this.state.inputValue}
style={{ width:'250px',marginRight:'10px',marginLeft:'10px'}}
onChange={this.changeInputValue}
/>
<Button
type="primary"
onClick={this.clickButton}
>提交</Button>
</div>
<div style={{ margin:'10px',width:'300px'}}>
<List
bordered
dataSource={this.state.list}
renderItem={(item,index) => (<List.Item onClick={this.removeList.bind(this,index)}>{item}</List.Item>)} />
</div>
</div>
);
}
changeInputValue(e){
const action = {
type:'changeInput',
value:e.target.value
};
store.dispatch(action);
this.setState(store.getState())
}
storeChange(){
this.setState(store.getState())
}
clickButton(e){
const action={type:'addItem'};
store.dispatch(action);
//this.setState(store.getState());
}
removeList(index){
const action={type:'removeItem',value: index};
store.dispatch(action);
//this.setState(store.getState())
}
}
export default TodoList;
//src/store/index.js
import {createStore} from "redux";
import reducer from "./reducer";
const store = createStore(reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
export default store
//src/store/reducer.js
const defaultState={
inputValue:'Write Something',
list:['早8点开晨会,分配今天的任务','早9点和项目经理开需求沟通会','早9点和项目经理开需求沟通会']
};
export default (state = defaultState,action)=>{
if (action.type==='changeInput')
{
let newState = JSON.parse(JSON.stringify(state));
newState.inputValue=action.value;
return newState
}
if (action.type==='addItem')
{
let newState = JSON.parse(JSON.stringify(state));
newState.list.push(newState.inputValue);
newState.inputValue='';
return newState
}
if (action.type==='removeItem')
{
let newState = JSON.parse(JSON.stringify(state));
newState.list.splice(action.value,1);
return newState
}
return state
}