list组件
import React, { Component, Fragment } from "react";
import "antd/dist/antd.css";
import { Input, Button, List } from "antd";
import store from "./store/index";
class TodoList extends Component {
constructor(props) {
super(props);
this.state = store.getState();
this.storeChange = this.storeChange.bind(this);
store.subscribe(this.storeChange);
this.inputChange = this.inputChange.bind(this);
this.clickBtn = this.clickBtn.bind(this);
}
render() {
return (
<Fragment>
<div>
<Input
placeholder="请输入服务项目"
style={{ width: "250px", margin: "10px 0" }}
onChange={this.inputChange}
value={this.state.inputValue}
/>
<Button type="primary" onClick={this.clickBtn}>
增加服务
</Button>
</div>
<List
bordered
dataSource={this.state.list}
renderItem={(item, index) => (
// 此处第一个参数是this,第二个参数是index
<List.Item onClick={this.deleteItem.bind(this, index)}>
{item}
</List.Item>
)}
/>
</Fragment>
);
}
storeChange() {
this.setState(store.getState());
}
inputChange(e) {
const action = {
type: "input_change",
value: e.target.value,
};
store.dispatch(action);
}
clickBtn() {
const action = {
type: "addItem",
};
store.dispatch(action);
}
deleteItem(index) {
const action = {
type: "deleteItem",
index: index,
};
store.dispatch(action);
}
}
export default TodoList;
redux
index
import { createStore } from "redux";
import reducer from "./reducer";
const store = createStore(reducer);
export default store;
reducer
const defaultState = {
inputValue: "Write Something",
list: ["早上4点起床,锻炼身体", "中午下班游泳一小时"],
};
export default (state = defaultState, action) => {
if (action.type === "input_change") {
let newState = JSON.parse(JSON.stringify(state));
newState.inputValue = action.value;
return newState;
} else if (action.type === "addItem") {
let newState = JSON.parse(JSON.stringify(state));
newState.list.push(newState.inputValue);
newState.inputValue = "";
return newState;
} else if (action.type === "deleteItem") {
let newState = JSON.parse(JSON.stringify(state));
newState.list.splice(action.index, 1);
return newState;
}
return state;
};
ui与业务逻辑拆分
import React, { Component, Fragment } from "react";
import store from "./store/index";
import TodoListUi from "../src/components/ItemChild";
class TodoList extends Component {
constructor(props) {
super(props);
this.state = store.getState();
this.storeChange = this.storeChange.bind(this);
store.subscribe(this.storeChange);
this.inputChange = this.inputChange.bind(this);
this.clickBtn = this.clickBtn.bind(this);
this.deleteItem = this.deleteItem.bind(this);
}
render() {
return (
<Fragment>
<TodoListUi
inputValue={this.state.inputValue}
list={this.state.list}
inputChange={this.inputChange}
clickBtn={this.clickBtn}
deleteItem={this.deleteItem}
></TodoListUi>
</Fragment>
);
}
storeChange() {
this.setState(store.getState());
}
inputChange(e) {
const action = {
type: "input_change",
value: e.target.value,
};
store.dispatch(action);
}
clickBtn() {
const action = {
type: "addItem",
};
store.dispatch(action);
}
deleteItem(index) {
const action = {
type: "deleteItem",
index: index,
};
store.dispatch(action);
}
}
export default TodoList;
无状态子组件
import React, { Fragment } from "react";
import ProTypes from "prop-types";
import "antd/dist/antd.css";
import { Input, Button, List } from "antd";
const ItemChild = (props) => {
return (
<Fragment>
<div>
<Input
placeholder="请输入服务项目"
style={{ width: "250px", margin: "10px 0" }}
onChange={props.inputChange}
value={props.inputValue}
/>
<Button type="primary" onClick={props.clickBtn}>
增加服务
</Button>
</div>
<List
bordered
dataSource={props.list}
renderItem={(item, index) => (
<List.Item
onClick={() => {
this.props.deleteItem(index);
}}
>
{item}
</List.Item>
)}
/>
</Fragment>
);
};
ItemChild.propTypes = {
inputValue: ProTypes.string,
list: ProTypes.array,
inputChange: ProTypes.func,
deleteItem: ProTypes.func,
clickBtn: ProTypes.func,
};
export default ItemChild;