Thinking in React Implemented by Reagent
前言
本文是学习Thinking in React这一章后的记录,并且用Reagent实现其中的示例。
概要
一、构造恰当的数据结构
Since you’re often displaying a JSON data model to a user, you’ll find that if your model was built correctly, your UI (and therefore your component structure) will map nicely.
VDom让我们可以将Model到View的映射交出,而更专注于数据和数据结构本身,即是折腾数据和数据结构才是我们的主要工作。因此我们要设计出与View中组件结构对应的数据结构,然后将不符合该数据结构的数据做一系列转换,然后将数据交给React就好了。
居上所述那么可以知道,数据结构就依赖View的结构,那么如何设计View的结构呢?是采用Top-down还是Bottom-up的方式呢?对于小型应用我们直接采用Top-down即可,对于大型应用则采用Bottom-up更合适。(根据过往经验将大规模的问题域拆分成多个小规模的问题域,然后对小问题域采用Top-down方式,若无法直接采用Top-down方式则继续拆分,然后将多个小问题域的值域组合即可得到大问题域的值域)
无论是Top-down还是Bottom-up方式,都要将View构建为树结构(这很符合DOM结构嘛)。因此得到如下结构
FilterableProductTable
|_SearchBar
|_ProductTable
|_ProductCategoryRow
|_ProductRow
而数据则从顶层View组件往下流动,各层提取各自数据进行渲染。
二、从静态非交互版本开始
It’s best to decouple these processes because building a static version requires a lot of typing and no thinking, and adding interactivity requires a lot of thinking and not a lot of typing.
从设计(他人或自己)那得到设计稿或HTML模板,我们就可以开始着手重构模板、添加交互效果和填充业务逻辑和服务端交互等功能了。且慢,我们先不着急动手,而是要先分清工作步骤,才能有条不紊地包质保量工作哦!
- 目标:得到符合React规范的View结构
- 目标:得到最低标准的可交互的React应用
- 目标:补充业务逻辑,细化交互
- 目标:连接远程数据源,细化交互
(ns demo.core
(:require [reagent.core :as re])
(def products [
{:category "Sporting Goods", :price "$49.99", :stocked true, :name "Football"}
{:category "Sporting Goods", :price "$9.99", :stocked true, :name "Baseball"}
{:category "Sporting Goods", :price "$29.99", :stocked false, :name "Basketball"}
{:category "Electronics", :price "$99.99", :stocked true, :name "iPod Touch"}
{:category "Electronics", :price "$399.99", :stocked false, :name "iPhone 5"}
{:category "Electronics", :price "$199.99", :stocked true, :name "Nexus 7"}
])
(declare <filterable-product-table>
<search-bar>
<product-table>
<product-category-row>
<product-row>)
(declare get-rows)
(defn <filterable-product-table>
[products]
[:div
[<search-bar>]
[<product-table> products]])
(defn <search-bar>
[]
[:form
[:input {:placeholder "Search..."}]
[:input {:type "checkbox"}]
"Only show products in stock."])
(defn <product-table>
[products]
[:table
[:thead
[:tr
[:th "Name"]
[:th "Price"]]]
[:tbody
(get-rows products)]])
(defn assemble-rows
[products]
(reduce
(fn [{:keys [cate] :as rows-info} product]
(let [curr-cate (:category product)
curr-rows (if (not= curr-cate cate)
(list ^{:key curr-cate}[<product-category-row> curr-cate])
(list))
rows (cons ^{:key (:name product)} [<product-row> product] curr-rows)]
(-> rows-info
(assoc :cate curr-cate) ;; 更新cate值
(update
:rows
(fn [o rows]
(concat rows o))
rows)))) ;; 更新rows值
{:cate nil :rows '()}
products))
(defn get-rows
[products]
(-> (assemble-rows products)
:rows
reverse))
(defn <product-category-row>
[cate]
[:tr
[:td {:colSpan 2} cate]])
(defn <product-row>
[product]
[:tr
[:td (when (:stocked product) {:style {:color "red"}})
(:name product)]
[:td (:price product)]])

这一步我们并没有提供交互功能,因此只会用到prop传递数据,绝对不会用到state的。而交互的意思是,对View的操作会影响应用数据,从而刷新View。
三、追加交互代码
交互实质上就是触发View状态变化,那么就必须提供一种容器来暂存当前View的状态,而这个在React就是state了。
(ns demo.core
(:require [reagent.core :as re])
(def products [
{:category "Sporting Goods", :price "$49.99", :stocked true, :name "Football"}
{:category "Sporting Goods", :price "$9.99", :stocked true, :name "Baseball"}
{:category "Sporting Goods", :price "$29.99", :stocked false, :name "Basketball"}
{:category "Electronics", :price "$99.99", :stocked true, :name "iPod Touch"}
{:category "Electronics", :price "$399.99", :stocked false, :name "iPhone 5"}
{:category "Electronics", :price "$199.99", :stocked true, :name "Nexus 7"}
])
(declare <filterable-product-table>
<search-bar>
<product-table>
<product-category-row>
<product-row>)
(declare get-rows
validate-product)
(defn <filterable-product-table>
[products]
(let [search-text (re/atom "")
stocked? (re/atom false)
on-search-text-change #(reset! search-text (.. % -target -value))
on-stocked?-change #(reset! stocked? (.. % -target -checked))]
(fn []
[:div
[<search-bar> on-search-text-change on-stocked?-change]
[<product-table> (filter (partial validate-product @search-text @stocked?) products)]])))
(defn validate-product
[search-text stocked? product]
(and (if stocked? (:stocked product) true)
(as-> search-text %
(re-pattern (str "(?i)" %))
(re-find % (:name product)))))
(defn <search-bar>
[on-search-text-change on-stocked?-change]
[:form
[:input {:placeholder "Search..."
:onChange on-search-text-change}]
[:input {:type "checkbox"
:onChange on-stocked?-change}]
"Only show products in stock."])
(defn <product-table>
[products]
[:table
[:thead
[:tr
[:th "Name"]
[:th "Price"]]]
[:tbody
(get-rows products)]])
(defn assemble-rows
[products]
(reduce
(fn [{:keys [cate] :as rows-info} product]
(let [curr-cate (:category product)
curr-rows (if (not= curr-cate cate)
(list ^{:key curr-cate}[<product-category-row> curr-cate])
(list))
rows (cons ^{:key (:name product)} [<product-row> product] curr-rows)]
(-> rows-info
(assoc :cate curr-cate) ;; 更新cate值
(update
:rows
(fn [o rows]
(concat rows o))
rows)))) ;; 更新rows值
{:cate nil :rows '()}
products))
(defn get-rows
[products]
(-> (assemble-rows products)
:rows
reverse))
(defn <product-category-row>
[cate]
[:tr
[:td {:colSpan 2} cate]])
(defn <product-row>
[product]
[:tr
[:td (when (:stocked product) {:style {:color "red"}})
(:name product)]
[:td (:price product)]])
注意:reagent中使用state时,需要定义一个返回函数的高阶函数来实现。
(defn <statefull-cmp> [data]
(let [local-state (re/atom nil)
on-change #(reset! local-state (.. % -target -value))]
(fn []
[:div
[:input {:onChange on-change}]
[:span @local-state]])))
(re/render [<statefull-cmp> 1]
(.querySelector js/document "#app"))
总结
尊重原创,转载请注明转自:http://www.cnblogs.com/fsjohnhuang/p/7692956.html _肥仔John
参考
https://reactjs.org/docs/thinking-in-react.html
Thinking in React Implemented by Reagent的更多相关文章
- react与jQuery对比,有空的时候再翻译一下
参考资料:http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-t ...
- [转] React Native Navigator — Navigating Like A Pro in React Native
There is a lot you can do with the React Native Navigator. Here, I will try to go over a few example ...
- React源码解析:ReactElement
ReactElement算是React源码中比较简单的部分了,直接看源码: var ReactElement = function(type, key, ref, self, source, owne ...
- react 插槽(Portals)
前言: 昨天刚看了插槽,以为可以解决我工作中遇到的问题,非常激动,我今天又仔细想了想,发现并不能解决... 不过还是记录一下插槽吧,加深印象,嗯,就酱. 插槽作用: 插槽即:ReactDOM.crea ...
- 关于clojurescript+phantomjs+react的一些探索
这两天需要使用phantomjs+react生成些图片 React->Clojurescript: 最开始发现clojurescript中包裹react的还挺多: https://github. ...
- React高级指南
高级指南 1.深入JSX: 从本质上讲,JSX 只是为 React.createElement(component, props, ...children) 函数提供的语法糖. 因为 JSX 被编译为 ...
- react解析markdown文件
当当当又get到了一个新技能,使用react-markdown来直接解析markdown文件(咳咳,小菜鸟的自娱自乐) 项目中遇到了一个API的那种展示方式,类似于入门手册啥的那种,如果是一个个调用接 ...
- Wait… What Happens When my React Native Application Starts? — An In-depth Look Inside React Native
Discover how React Native functions internally, and what it does for you without you knowing it. Dis ...
- React源码 ReactElement
我们的JSX里面标签,属性,内容都会传递到React.createElement()这个方法里面.那么这个方法他到底有什么意义以及他的返回,我们叫他ReactElement.他到底有什么样的作用 /* ...
随机推荐
- 集美大学网络1413第十五次作业成绩(团队十) -- 项目复审与事后分析(Beta版本)
题目 团队作业10--项目复审与事后分析(Beta版本) 团队作业10成绩 --团队作业10-1 Beta事后诸葛亮 团队/分值 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色 ...
- 团队作业8——Beta 阶段冲刺3rd day
一.当天站立式会议 二.每个人的工作 (1)昨天已完成的工作(具体在表格中) 添加了订单功能,并且对订单功能进行了测试 (2)今天计划完成的工作(具体如下) 添加支付功能 (3) 工作中遇到的困难(在 ...
- 201521123026 《Java程序设计》第一周学习总结
1. 本章学习总结 1.简要了解JAVA的发展史以及其特点(面向对象.跨平台性,健壮性,安全性,可移植性,多线程性,动态性等) 2.认识JAVA三大平台(Java SE,Java EE,JavaME) ...
- 201521123037 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. java异常继承架构 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果( ...
- JAVA课程设计+五子棋(个人博客)
1.团队博客地址: http://www.cnblogs.com/yzb123/p/7063424.html 2.个人负责模块或任务说明 游戏初始化,清除棋盘上的棋子 鼠标监听器 棋子落棋 判断胜负 ...
- java: Java中this和super的用法总结
this this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. this的用法在java中大体可以分为3种: 1.普通的直接引用 这种就不用讲了,this相当于是指向当前对象本 ...
- javascript:12种JavaScript MVC框架之比较
Gordon L. Hempton是西雅图的一位黑客和设计师,他花费了几个月的时间研究和比较了12种流行的JavaScript MVC框架,并在博客中总结了每种框架的优缺点,最终的结果是,Ember. ...
- JavaScript随笔
文档模式 主要模式2中混杂模式和标准模式. 1混杂模式,混杂模式会让IE的行为与(包含非标准特性的)IE5相同. 2标准模式,标准模式让IE的行为更接近标准行为. 准标准模式:通过过渡型或框架集型触发 ...
- Java内存分配之堆、栈和常量池
Java内存分配主要包括以下几个区域: 1. 寄存器:我们在程序中无法控制 2. 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 3. 堆:存放用new产生的数据 4. 静 ...
- 手机管家iPhoneX的适配总结
WeTest 导读 随着苹果发布会的结束,Xcode的GM版也上线了,也意味着iPhoneX适配之旅的开始. 一.设计关注篇 注意设计的基本原则:(苹果呼吁的) 规格原帖:https://develo ...