欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList

這篇“React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList”文章吧。

為鐘祥等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及鐘祥網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、鐘祥網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

功能樣式

React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList

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

配置文件

使用Webpack構(gòu)建的文件夾如下

React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList

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字符串來決定是否過濾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字符串來決定三個(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;
 }
 }
 }
}

以上就是關(guān)于“React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站名稱:React+Redux怎么實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList
本文路徑:http://chinadenli.net/article10/gsjido.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)外貿(mào)網(wǎng)站建設(shè)小程序開發(fā)響應(yīng)式網(wǎng)站域名注冊(cè)動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)