React+Redux實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList
使用Redux做了一個(gè)簡(jiǎn)單的ToDoList待辦事項(xiàng)列表,具體如下
這個(gè)例子也是源于Redux作者Dan Abramov的視頻demo
還要特別說明一下
我還沒有使用react-redux庫(kù)進(jìn)行解耦(可能以后加)
也沒有拆分成多個(gè)文件等等優(yōu)化
為了單純的練習(xí)redux
適合初步學(xué)習(xí)redux的同學(xué)
本人學(xué)疏才淺,發(fā)現(xiàn)可以優(yōu)化的地方或者問題還請(qǐng)大家指正,謝謝
功能樣式

樣子就是這樣的
在輸入框輸入待辦事項(xiàng)
功能很簡(jiǎn)單
鼠標(biāo)點(diǎn)擊Add或者鍵盤按下Enter輸出
ShowAll顯示全部待辦事項(xiàng)
ShowActive顯示未完成的待辦事項(xiàng)(未劃掉的)
ShowCrossed顯示已完成的待辦事項(xiàng)(劃掉的)
配置文件
使用Webpack構(gòu)建的文件夾如下

webpack.config.js配置文件
module.exports = {
entry: {
index: './src/js/entry.js'
},
output: {
path: './static/dist/',
publicPath: 'http://localhost:8080/static/dist/',
filename: '[name].js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude:/node_modules/,
query: {
presets: ['react', 'es2015']
}
},
{
test: /.less$/,
loader: 'style!css!less'
}
]
}
}
package.json的依賴項(xiàng)
{
"name": "react-demo",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"diy": "webpack-dev-server --progress --colors --devtool sourcemap"
},
"author": "Payson",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"css-loader": "^0.26.1",
"jquery": "^3.1.1",
"less": "^2.7.2",
"less-loader": "^2.2.3",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-redux": "^5.0.2",
"redux": "^3.6.0",
"style-loader": "^0.13.1",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
}
}
html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>React</title> </head> <body> <div id="root"></div> <script src="http://localhost:8080/static/dist/index.js"></script> </body> </html>
腳本文件
沒有細(xì)拆文件
直接寫在入口文件entry.js了
注釋就寫在代碼里了
require('../less/index.less'); //行間樣式受限制不能添加偽類偽元素,所以還是添加了less(css)控制樣式
import React from 'react';
import {Component} from 'react'
import ReactDom from 'react-dom';
import {createStore, combineReducers} from 'redux';
class ToDoList extends Component {
addHandler(){ //添加待辦事項(xiàng)的listener
let Inp = this.refs.Inp; //獲取真實(shí)DOM的輸入value
if(!Inp.value){ //如果沒有輸入值,直接返回
return;
}
store.dispatch( //dispatch一個(gè)添加項(xiàng)目的action,并傳入輸入數(shù)據(jù)
{
type: 'ADD_ITEM',
newItem: Inp.value
}
)
Inp.value = ''; //提交后,清空輸入
Inp.focus(); //重置輸入焦點(diǎn)
}
toggleHandler(item){ //Action Creator:負(fù)責(zé)提交切換中劃線的action
store.dispatch(
{
type: 'TOGGLE_ITEM',
changeID: item.ID
}
);
}
showAllHandler(){ //Action Creator:負(fù)責(zé)showAll的action
store.dispatch(
{
type: 'SET_FILTER',
filter: 'SHOW_ALL'
}
);
}
showActiveHandler(){ //Action Creator:負(fù)責(zé)showActive的action
store.dispatch(
{
type: 'SET_FILTER',
filter: 'SHOW_ACTIVE'
}
);
}
showCrossedHandler(){ //Action Creator:負(fù)責(zé)showCrossed的action
store.dispatch(
{
type: 'SET_FILTER',
filter: 'SHOW_CROSSED'
}
);
}
render(){ //渲染結(jié)構(gòu)樣式
let _this = this; //緩存this
let state = store.getState(); //緩存store的快照--state
let {list, option} = state; //解構(gòu)賦值獲取兩個(gè)子state
//list是一個(gè)數(shù)組,內(nèi)部數(shù)組元素是對(duì)象表示每一個(gè)列表項(xiàng)
//option是一個(gè)字符串,表示當(dāng)先選擇的選項(xiàng)
switch(option){ //通過判斷當(dāng)前的option字符串來(lái)決定是否過濾list數(shù)組
case 'SHOW_ACTIVE':
list = list.filter(function(item){
return !item.del;
});
break;
case 'SHOW_CROSSED':
list = list.filter(function(item){
return item.del;
});
break;
}
document.body.addEventListener('keydown', function(e){
if(e.which == 13){
_this.addHandler();
}
}); //綁定鍵盤enter事件
return (
<div>
<input type="text" ref="Inp"/> //設(shè)置ref屬性為了獲取真實(shí)DOM節(jié)點(diǎn)
<button onClick={_this.addHandler.bind(_this)}>Add</button>
<ul className="option">
<li onClick={_this.showAllHandler.bind(_this)}>
<span style={{textDecoration: option!='SHOW_ALL' ? 'underline' : 'none'}}>ShowAll</span>
</li>
<li onClick={_this.showActiveHandler.bind(_this)}>
<span style={{textDecoration: option!='SHOW_ACTIVE' ? 'underline' : 'none'}}>ShowActive</span>
</li>
<li onClick={_this.showCrossedHandler.bind(_this)}>
<span style={{textDecoration: option!='SHOW_CROSSED' ? 'underline' : 'none'}}>ShowCrossed</span>
</li> //判斷option字符串來(lái)決定三個(gè)選項(xiàng)的樣式
</ul>
<ul className="list">
{
list.map(function(item, index){ //通過list數(shù)組map映射為虛擬DOM節(jié)點(diǎn)
return <li key={index}>
<span style={{textDecoration: item.del ? 'line-through': 'none'}}
onClick={_this.toggleHandler.bind(_this, item)}>{item.item}</span>
</li>
})
}
</ul>
</div>
)
}
}
const list = (state = [], action) => { //list-reducer
switch(action.type){
case 'ADD_ITEM':
return [
...state,
{
item: action.newItem, //列表項(xiàng)內(nèi)容
ID: state.length, //列表項(xiàng)ID
del: false //列表項(xiàng)是否已劃掉
}
];
case 'TOGGLE_ITEM':
return state.map((item)=>{
return Object.assign({},item,{
del: action.changeID == item.ID ? !item.del : item.del
});
});
default:
return state;
}
}
const option = (state = 'SHOW_ALL', action) => { //option-reducer
switch(action.type){
case 'SET_FILTER':
return action.filter;
default:
return state;
}
}
const reducer = combineReducers({list, option}); //利用redux庫(kù)API-combineReducers()合并reducer
const store = createStore(reducer); //利用redux庫(kù)API-createStore()創(chuàng)建store
const render = () => { //自定義的渲染函數(shù)
ReactDom.render(
<ToDoList/>,
document.getElementById('root')
);
}
store.subscribe(render); //綁定render函數(shù),每次state更新時(shí)執(zhí)行
render(); //首次渲染
樣式文件
index.less文件加一些樣式控制
.option {
list-style-type: none;
padding: 0;
margin-top: 5px;
font-size: 13px;
li {
float: left;
margin-right: 15px;
span {
cursor: pointer;
font-weight: bold;
}
}
&::after {
content: '';
display: block;
clear: both;
}
}
.list {
li {
span {
&:hover {
color: #f40;
cursor: pointer;
}
&::selection {
color: #000;
background-color: #fff;
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React?Hook實(shí)現(xiàn)對(duì)話框組件
這篇文章主要為大家詳細(xì)介紹了React?Hook實(shí)現(xiàn)對(duì)話框組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
ahooks控制時(shí)機(jī)的hook實(shí)現(xiàn)方法
這篇文章主要為大家介紹了ahooks控制時(shí)機(jī)的hook實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
30行代碼實(shí)現(xiàn)React雙向綁定hook的示例代碼
本文主要介紹了30行代碼實(shí)現(xiàn)React雙向綁定hook的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
React?Hook?Form?優(yōu)雅處理表單使用指南
這篇文章主要為大家介紹了React?Hook?Form?優(yōu)雅處理表單使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
react實(shí)現(xiàn)動(dòng)態(tài)增減表單項(xiàng)的示例代碼
在做項(xiàng)目的時(shí)候,甲方給的信息有限,網(wǎng)頁(yè)的備案信息寫成固定的,之后驗(yàn)收的時(shí)候,甲方要求把這個(gè)備案信息寫成動(dòng)態(tài)的,可以自增減,下面通過實(shí)例代碼給大家介紹react實(shí)現(xiàn)動(dòng)態(tài)增減表單項(xiàng)的示例,感興趣的朋友跟隨小編一起看看吧2024-05-05
React Native時(shí)間轉(zhuǎn)換格式工具類分享
這篇文章主要為大家分享了React Native時(shí)間轉(zhuǎn)換格式工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
React18使用Echarts和MUI實(shí)現(xiàn)一個(gè)交互性的溫度計(jì)
這篇文章我們將結(jié)合使用React 18、Echarts和MUI(Material-UI)庫(kù),展示如何實(shí)現(xiàn)一個(gè)交互性的溫度計(jì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
利用React-router+Webpack快速構(gòu)建react程序
目前 React、Webpack 等技術(shù)如火如荼,你是不是還在愁苦如何把這些雜亂的知識(shí)怎么學(xué)習(xí)一下,開啟一段新的前端開發(fā)之路呢?那么這篇將給大家運(yùn)用示例代碼詳細(xì)的介紹使用React-router和Webpack如何快速構(gòu)建一個(gè)react程序,感興趣的朋友們下面來(lái)一起看看吧。2016-10-10
react實(shí)現(xiàn)菜單權(quán)限控制的方法
本篇文章主要介紹了react實(shí)現(xiàn)菜單權(quán)限控制的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-12-12

