Redux的使用
主要入口: src创建store文件夹并创建index.js
import { createStore } from "redux";
import listRedux from "./listRedux";
const store = createStore(listRedux);
export default store;
store文件夹创建listActionTypes.js
export const CHANGE_INPUT_VALUE = 'change_input_value';
export const INPUT_SUBMIT = 'input_submit';
export const DELETE_ITEM = 'delete_item';
store文件夹创建listActionCreators.js
import { CHANGE_INPUT_VALUE, INPUT_SUBMIT, DELETE_ITEM } from "./listActionTypes";
export const getInputChangeAction = (value) => ({
type: CHANGE_INPUT_VALUE,
value
});
export const getInputSubmit = (value) => ({
type: INPUT_SUBMIT,
value
});
export const listReduxDeleteItem = (index) => ({
type: DELETE_ITEM,
index
});
store文件夹创建listRedux.js
因为会用到很多redux、做个简单的区分、防止混淆
// 处理list的Redux
// 引用规范
import { CHANGE_INPUT_VALUE, INPUT_SUBMIT, DELETE_ITEM } from "./listActionTypes";
// 默认值
let defaultState = {
inputValue: '',
list: [ '测试1', '测试2']
};
export default (state = defaultState, action) => {
// input修改值
if(action.type === CHANGE_INPUT_VALUE){
const newState = JSON.parse(JSON.stringify(state));
newState.inputValue = action.value;
// 替换掉原来的值
return newState;
}
// input提交
if(action.type === INPUT_SUBMIT){
const newState = JSON.parse(JSON.stringify(state));
newState.list.push(action.value);
newState.inputValue = '';
return newState
}
// item删除
if (action.type === DELETE_ITEM){
let newState = JSON.parse(JSON.stringify(state));
newState.list.splice(action.index, 1);
return newState;
}
return state;
}
todoList子组件:input 组件使用store
import React, { Component } from "react";
import { Input } from 'antd';
import store from "../../../store";
import { getInputChangeAction } from "../../../store/listActionCreators";
class ListInput extends Component{
constructor(props) {
super(props);
this.state = store.getState();
store.subscribe(this.listenStoreChange)
}
listenStoreChange = () =>{
this.setState(store.getState());
};
inputChange(event){
const action = getInputChangeAction(event.target.value);
store.dispatch(action)
};
render() {
let { inputValue } = this.state;
return(
<Input
placeholder="测试"
value={ inputValue }
onChange={(event) => this.inputChange(event)}
/>
)
}
}
export default ListInput;
todoList子组件:Li 组件使用store
import React, { Component } from "react";
import store from "../../../store";
import { listReduxDeleteItem } from '../../../store/listActionCreators';
class ListLi extends Component{
liClick() {
console.log(this.props.index);
let action = listReduxDeleteItem(this.props.index);
store.dispatch(action);
};
render() {
const { item } = this.props;
return(
<li onClick={() => this.liClick()}>
{item}
</li>
)
}
}
export default ListLi
todoList父组件: List组件使用store
import React, { Component } from "react";
import ListInput from "./components/ListInput";
import ListLi from './components/ListLi'
import { Card, Button, notification } from "antd";
import store from "../../store";
import { getInputSubmit } from "../../store/listActionCreators";
class Index extends Component{
constructor(props) {
super(props);
this.state = store.getState();
store.subscribe(this.listenStoreChange)
};
openNotification = () => {
notification.open({
message: '提示',
description:
'输入不可为空哦~',
onClick: () => {
console.log('Notification Clicked!');
},
});
};
listenStoreChange = () => {
this.setState(store.getState());
};
addItem = () => {
const { inputValue } = this.state;
if(!inputValue){
this.openNotification();
return;
}
const action = getInputSubmit(inputValue);
store.dispatch(action);
};
render() {
let { list } = this.state;
const listLi = list.map((item, index) => {
return (
<ListLi key={index} item={item} index={index} />
)
});
const noLi = '暂无更多';
return (
<div style={{width: '100%'}}>
<Card title='todo' style={{marginBottom: '30px'}}>
<h3>List2 Redux</h3>
<ListInput />
<Button type="primary" style={{marginTop: '20px'}} onClick={this.addItem}>添加</Button>
</Card>
<Card title='列表' style={{marginBottom: '30px'}}>
<ul>
{
list.length > 0 ? listLi : noLi
}
</ul>
</Card>
</div>
)
}
}
export default Index