這篇文章主要介紹了React-Reflux的基礎介紹,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
因為工作需要使用React Reflux 開發,最近幾天都在努力學習著,特別是Reflux,在網上查找的許多資料和github 上的文檔年代都有點久遠,JavaScript 按照目前的節奏,更新得太快,舊文檔的一些文法跟不上更新,對廣大初學者來說,確實存在著許多困惑。本文是僅適於初學者或對 React 有興趣的朋友,大神請繞道! ! !
廢話不多說,進入正題~
引用俗話:“學語言用hello world,學框架用todolist”,今天咱們就用todolist 來做說明。
作為一個對比todolist-raw 是沒有用Reflux 實現todolist 的增加和刪除的,react-reflux 使用Reflux 實現todolist 的增加和刪除,react- reflux 再分基本關聯和簡單法關聯Component。
先看下元件的效果圖:
電腦需要準備好環境,我用的是create-react-app
和yarn ,請自行百度安裝好這兩個工具,專案目錄精簡如下圖:
# 1. todolist-raw 就幾個文件,裡面的程式碼都比較簡單,相信各道友一看就懂了。 package.json 的設定如下:
{
"name": "todolist-raw",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
yarn 安裝了Rflux 之後,提示 "react-scripts"
不是內部指令,yarn start 啟動不了,反覆試驗都不行,後來打扣了 yarn -h
然後接著yarn -autoclean 就好了。講了那麼多,其實我遇到的這個問題也不算版本不對稱的問題吧,建議出現一些莫名其妙的 bug 就整理下 yarn。癢?癢就撓嗆! ! !
index.html 如下:與自動產生的沒什麼改變,就是刪了一些註解! <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>TodoList-raw</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
</body>
</html>
index.js 如下:1 import React from 'react';
2 import ReactDOM from 'react-dom';
3 import TodoList from './TodoList';
4
5 ReactDOM.render(<TodoList />, document.getElementById('root'));
#View CodeTodoList.js 如下: import React, { Component, Fragment } from 'react';
import Items from './Items';
class TodoList extends Component {
constructor(props) {
super(props);
this.state = {
items: [1, 2, 3],
isC: false
};
this.handlerClick = this.handlerClick.bind(this);
}
// 添加一个 item 项
handlerClick() {
const val = this.refs.inputEl.value;
this.refs.inputEl.value = '';
const items = [...this.state.items, val]; // 最好不要直接操作 state
this.setState({items}); // es6 语法,等价于 {items: items}
}
// 删除某一个 item 项,点击就删除
handlerDelete(index) {
const items = [...this.state.items];
items.splice(index, 1);
this.setState({items});
}
render() {
return (
<Fragment>
<h3 id="Hello-nbsp-React">Hello React!</h3>
<input
ref="inputEl"
style={{marginRight: "10px"}}
/>
<button onClick={this.handlerClick}>添加</button>
<Items
msg={this.state.items}
func={this.handlerDelete.bind(this)} />
</Fragment>
);
}
}
export default TodoList;
說明幾點:
b. 代码很少阅读起来应该没难度,而且上面也有一些注释了。不过还是要啰嗦一下。React 是用数据(data)基于状态(state)来驱动视图(view),也就是搞来搞去都是在搞数据,用数据改变状态来达到渲染视图的目的,大多是在虚拟内存处理,通过什么 diff 比较算法,层层计较然后达到提高性能,加快视图渲染的目的。额,我只能用“非常快”来形容 React 的性能,当然,性能的事还是跟实际代码复杂度相关,我只想说 React 确实出众!扯远了...,既然是基于状态(state),所以要有 state,在 constructor 中定义了,而且处理业务时改变 state,比如 handlerClick,deleteClick 中都是先用变量保存下来,通过 this.setState() 方法在设置回去!
c. 父子关系的组件,父组件可通过属性来给子组件传参,就像这样:Items msg={this.state.items} />,子组件可通过 this.props.msg 拿到 items。
Items.js 代码如下:
import React, { Component } from 'react'; class Items extends Component { constructor(props) { super(props); this.func = this.props.func; } render() { return ( <ul> {this.props.msg.map((item, index) => <li key={index} onClick={this.func.bind(this, index)} >{item}</li> )} </ul> ); } } export default Items;
注意,父组件传递方法给子组件时,this 指向的是子组件,所以通过属性传递时,需要用函数绑定 bind() 绑定父组件的 this。
最后,通过 cmd 命令行,yarn start 运行任务即可实现一个简单的 TodoList 功能。
2. react-reflux: 通过 Reflux 来实现简单的 TodoList (基本法关联 Component)。
2.1 简单地说明下为什么要 Reflux 架构,如图(自己画的组件树结构!),不存在父子关系的组件(如 B 和 E)通讯势必会很困难而且麻烦,需要一个中间件(Store)来存储数据,类似于订阅者和发布者(中间人模式)一样,大家都往 Store 中存取数据就好,不需要层层走关系了,避免了层层传递数据的灾难!
2.2 关于 Actions、Store 和 Component 的关系,可以这么理解:Store 作为一个中间人有订阅和发布的功能,Actions 就是 Component 触发的动作,Actions 被 Store 监听着,组件有新动作了, Store 就会做出相应的处理(回调函数)更改 Store 中的数据通过 this.trigger(this.items) 发布消息[就相当于更新 items], Component 监听着 Store,用一些手段(mixin 或回调函数)关联起 Component 中的状态信息(state.items)和 Store 中的信息(items),这就是所谓的单向数据流,单向表示一个方向流动,不能反向。
2.3 目录结构和前面的 todolist-raw 没多大区别,就是多了 TodoActions.js 和 TodoStore.js 两个文件。如图:
2.4 一言不合就贴代码。首先 index.js 代码没变化,就不贴了,TodoActions.js 代码如下:
import Reflux from 'reflux'; const TodoActions = Reflux.createActions([ 'getAll', 'addItem', 'deleteItem' ]); export default TodoActions;
TodoStore.js 代码如下:
import Reflux from 'reflux'; import Actions from './TodoActions'; const TodoStore = Reflux.createStore({ // items: [1, 2, 3], // listenables: Actions, init() { console.log('TodoStore init method~'); this.items = [1, 2, 3]; // 给个初始值 this.listenables = Actions; // 监听 TodoActions.js // this.listenTo(addItem, 'addItem'); }, onGetAll() { console.log('onGetAll'); this.trigger(this.items); }, onAddItem(model) { console.log('onAddItem---', model); this.items.unshift(model); this.trigger(this.items); }, onDeleteItem(index) { console.log('onDeleteItem---', index); this.items.splice(index, 1); this.trigger(this.items); } }) export default TodoStore;
说明:多个监听用 listenables,单个监听 this.listenTo(addItem, 'addItem'); 多个监听的时候定义处理函数是 on + ActionName 驼峰式命名。定义初始值和监听可以写在 init 方法外面,就像上面那样(已注释)。
2.5 Actions 和 Store 都写好了,就差最后一步,整合到组件 Component 中,才算有点意义了!TodoList.js 代码如下:
import React, { Component, Fragment } from 'react'; import Actions from './TodoActions'; import TodoStore from './TodoStore'; import Items from './Items'; class TodoList extends Component { constructor(props) { super(props); this.state = { items: [], isC: false }; this.handlerClick = this.handlerClick.bind(this); } // 组件挂载 componentDidMount() { this.unsubscribe = TodoStore.listen(this.onStatusChange, this); Actions.getAll(); } // 组件移除 componentWillUnmount() { console.log('componentWillUnmount'); this.unsubscribe(); // 解除监听 } // callback onStatusChange(items) { this.setState({items}); } // 添加一个 item 项 handlerClick() { const val = this.refs.inputEl.value; this.refs.inputEl.value = ''; Actions.addItem(val); } render() { return ( <Fragment> <h3 id="Hello-nbsp-React-Reflux">Hello React-Reflux!</h3> <input ref="inputEl" style={{marginRight: "10px"}} /> <button onClick={this.handlerClick}>添加</button> <Items msg={this.state.items} /> </Fragment> ); } } export default TodoList;
说明:这是基本的添加关联,需要在组件挂载时监听 Store,需要定义一个回调函数 onStatusChange(),组件卸载时解除监听 this.unsubscribe(),Store 源码如下:
不传 bindContext 更新不了状态,回调函数 onStatusChange 中报异常,传入 this 就好了。如图:
Items.js 代码如下:
import React, { Component } from 'react'; import Actions from './TodoActions'; class Items extends Component { render() { return ( <ul> {this.props.msg.map((item, index) => <li key={index} onClick={this.handlerDelete.bind(this, index)} >{item}</li> )} </ul> ); } handlerDelete(index) { Actions.deleteItem(index); } } export default Items;
3. react-reflux: 通过 Reflux 来实现简单的 TodoList (简便法关联 Component)。
3.1 先安装 react-mixin 和 axios: npm install react-mixin,npm install axios。结合异步操作实现简便 Store 关联 Component。
安装两个依赖之后,修改 TodoStore.js 和 TodoList.js 代码如下:
我贴:TodoStore.js:
import Reflux from 'reflux'; import Actions from './TodoActions'; import Axios from 'axios'; const TodoStore = Reflux.createStore({ // items: [3, 2, 1], // listenables: Actions, init() { console.log('TodoStore init method~'); this.items = [3, 2, 1]; // 初始值,此处不是 state, mark-1 -2 -3 都可以直接操作 this.listenables = Actions; }, onGetAll() { console.log('onGetAll'); Axios.get('https://api.github.com/') .then(res => { const keys = Object.keys(res.data); console.log('axios-response-keys: ', keys); this.items = keys; // mark-1 this.trigger(this.items); }) .catch(err => console.log('axios-error: ', err)); }, onAddItem(model) { console.log('onAddItem---', model); this.items.unshift(model); // mark-2 console.log('TodoStore-items: ', this.items); this.trigger(this.items); }, onDeleteItem(index) { console.log('onDeleteItem---', index); this.items.splice(index, 1); // mark-3 this.trigger(this.items); } }) export default TodoStore;
我再贴:TodoList.js:
import React, { Component, Fragment } from 'react'; import ReactMixin from 'react-mixin'; import Reflux from 'reflux'; import Actions from './TodoActions'; import TodoStore from './TodoStore'; import Items from './Items'; class TodoList extends Component { constructor(props) { super(props); this.state = { items: [], isC: false }; this.handlerClick = this.handlerClick.bind(this); } // 组件挂载 componentDidMount() { Actions.getAll(); } // 添加一个 item 项 handlerClick() { const val = this.refs.inputEl.value; this.refs.inputEl.value = ''; if (!val) { alert('Please enter the data which type of number or string'); return false; } Actions.addItem(val); } render() { return ( <Fragment> <h3 id="Hello-nbsp-React-Reflux">Hello React-Reflux!</h3> <input ref="inputEl" style={{marginRight: "10px"}} /> <button onClick={this.handlerClick}>添加</button> <Items msg={this.state.items} /> </Fragment> ); } } // 用 Reflux.connect 将 Store 和 Component 组合在一起 ReactMixin.onClass(TodoList, Reflux.connect(TodoStore, 'items')); export default TodoList;
修改之后 yarn start 启动项目,截图如下:
4. 说在后面的话:本篇文章只是关于 React-Reflux 基础入门的一些知识,没有涉及实战应用,读者自酌。对于 React 我也是初学者,难免有许多不准确的地方,有待提高,欢迎各位道友留言指正。
4.1 好了,简单地分享就到此结束了,谢谢阅读~~~
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是React-Reflux的基礎介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript是現代網站的核心,因為它增強了網頁的交互性和動態性。 1)它允許在不刷新頁面的情況下改變內容,2)通過DOMAPI操作網頁,3)支持複雜的交互效果如動畫和拖放,4)優化性能和最佳實踐提高用戶體驗。

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)