前段時間實現了React + Antd實現簡單的todolist,組件之間的傳值經過props層層傳遞太過繁瑣,此次改進了項目引入了redux進行狀態的管理。項目地址react
yarn add redux
複製代碼
import { createStore } from "redux";
const reducer = function(state = [], action) {
return state;
}
const store = createStore(reducer);
複製代碼
export const ADD_TODO = 'add_todo' // 增
export const DELETE_TODO = 'delete_todo' // 刪
export const COMPLETE_TODO = 'complete_todo' // 改
export const SEARCH_TODO = 'search_todo' // 查
複製代碼
import {ADD_TODO, DELETE_TODO, COMPLETE_TODO, SEARCH_TODO} from '../constants'
export function addTodo (todo) {
return {
type: ADD_TODO,
todo
}
}
export function deleteTodo (id) {
return {
type: DELETE_TODO,
id
}
}
export function completeTodo (id) {
return {
type: COMPLETE_TODO,
id
}
}
export function searchTodo (text) {
return {
type: SEARCH_TODO,
text
}
}
複製代碼
import {ADD_TODO, DELETE_TODO, COMPLETE_TODO, SEARCH_TODO} from '../constants'
let initState = [
{
id: 1,
text: 'test123',
completed: true
},
{
id: 2,
text: 'testabc',
completed: false
},
{
id: 3,
text: 'done123',
completed: true
}
]
const todoReducer = (state = initState, action) => {
switch (action.type) {
case ADD_TODO:
return [...state, action.todo]
case DELETE_TODO:
return state.filter(item => {
return item.id !== action.id
})
case COMPLETE_TODO:
return state.map(item => {
let obj = action.id === item.id ? {...item, completed: !item.completed} : item
return obj
})
case SEARCH_TODO:
if (action.text === '') {
return state
} else {
return state.filter(item => {
return item.content.indexOf(action.text) !== -1
})
}
default:
return state
}
}
export default todoReducer
複製代碼
import {createStore} from 'redux'
import todoReducer from './reducers'
const store = createStore(todoReducer)
export default store
複製代碼
補充:若是有多個reducer,能夠這樣處理git
import { combineReducers } from 'redux';
import reducer1 from './reducers/reducer1'
import reducer2 from './reducers/reducer2'
const allReducers = {
reducer1,
reducer2
}
const rootReducer = combineReducers(allReducers);
let store = createStore(rootReducer);
複製代碼
yarn add redux-devtools-extension
複製代碼
import {createStore} from 'redux'
import todoReducer from './reducers'
import {composeWithDevTools } from 'redux-devtools-extension'
const store = createStore(todoReducer, composeWithDevTools())
export default store
複製代碼
import React from 'react';
import TodoList from './TodoList'
import store from './store'
import {addTodo} from './store/actions'
store.subscribe(() => {
console.log(store.getState())
})
store.dispatch(addTodo({id:4, text: 'nihao', completed: false}))
function App() {
return (
<div className="App">
<TodoList></TodoList>
</div>
);
}
export default App;
複製代碼
yarn add react-redux
複製代碼
import React, { Component } from 'react'
import TodoListApp from './components/TodoListApp'
import {Provider} from 'react-redux'
import store from './store'
class App extends Component {
render () {
return(
<Provider store={store}>
<TodoListApp/>
</Provider>
)
}
}
export default App
複製代碼
import React from 'react';
import { Typography, Button } from 'antd';
import './TodoItem.less'
const { Text } = Typography;
function TodoItem ({completed, id, content, handleChangeItem,handleDeleteItem}) {
return (
<div className="item-container" onDoubleClick={() => handleChangeItem(id)} style={{cursor: 'pointer'}}>
<Text delete={completed}>{content}</Text>
<Button type="primary" icon="delete" onClick={() => handleDeleteItem(id)}></Button>
</div>
)
}
export default TodoItem;
複製代碼
import {connect} from 'react-redux'
import TodoItemComponent from '../components/TodoItem'
import {completeTodo, deleteTodo} from '../store/actions'
const mapDispatchToProps = dispatch => {
return {
handleChangeItem: id => {
dispatch(completeTodo(id))
},
handleDeleteItem: id => {
dispatch(deleteTodo(id))
}
}
}
const TodoItemContainer = connect(null, mapDispatchToProps)(TodoItemComponent)
export default TodoItemContainer
複製代碼
import React from 'react';
import TodoItem from '../containers/TodoItem'
import { List } from 'antd';
function DataList ({list}) {
return (
<List
bordered
dataSource={list}
renderItem={item => (
<List.Item>
<TodoItem {...item}/>
</List.Item>
)}
/>
)
}
export default DataList;
複製代碼
import {connect} from 'react-redux'
import TodoListComponent from '../components/DataList'
const mapStateToProps = state => {
return {
list: state
}
}
const TodoListContainer = connect(mapStateToProps)(TodoListComponent)
export default TodoListContainer
複製代碼
其餘組件也按照上面展現組件和容器組件進行拆分詳情查看項目github
修改App.js,引入react-redux,被Provider包裹的組件都能拿到storeredux
import React from 'react';
import TodoListApp from './components/TodoListApp'
import store from './store'
import {Provider} from 'react-redux'
function App() {
return (
<Provider store={store}>
<TodoListApp></TodoListApp>
</Provider>
);
}
export default App;
複製代碼
相關連接:React + Antd實現簡單的todolistbash