Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
react 简介
Search
Yuheng Zhang
November 14, 2014
400
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
react 简介
react 简介
Yuheng Zhang
November 14, 2014
More Decks by Yuheng Zhang
See All by Yuheng Zhang
Unleash your Vim
zhangyuheng
0
260
Javascript 函数表达式
zhangyuheng
2
440
Featured
See All Featured
Crafting Experiences
bethany
1
190
From π to Pie charts
rasagy
0
220
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
400
Leo the Paperboy
mayatellez
7
1.9k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
72
40k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
180
The Invisible Side of Design
smashingmag
301
52k
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
340
Google's AI Overviews - The New Search
badams
0
1k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
Transcript
React 简介 宫煌 2014.11.14
题外话 about Yahoo http://yahooeng.tumblr.com/post/101682875656/evolving-yahoo-mail
React
React
Overview • 只关注 view 层 • ⻚页⾯面细划分为各部分 component • 核⼼心是组件的
render
Features • ⻚页⾯面渲染性能好,使⽤用 virtual DOM,避免操纵 DOM 损耗性能 • DOM 的读/写批量进⾏行,减少
layout 计算次数 • 当属性变化时,基于 virtual DOM 全部重新渲染 • 不需要借⽤用模版语⾔言实现逻辑,no strings • 不需要专⻔门操作 DOM 的库(jQuery)
var ExampleApplication = React.createClass({ render: function() { var appName =
this.props.appName; var message = 'React has been successfully running.' + ' Appname is: ' + appName; return React.DOM.p(null, message); } }); var ExampleApplicationFactory = React.createFactory(ExampleApplication); React.render( ExampleApplicationFactory({appName: 'Demo'}), document.getElementById('container') ); Basic usage
Full example <!DOCTYPE html> <html> <head> <meta http-equiv='Content-type' content='text/html; charset=utf-8'>
<title>Example</title> <link rel="stylesheet" href="../shared/css/base.css" /> </head> <body> <h1>Example</h1> <div id="container"> </div> <script src="../../build/react.js"></script> <script> var ExampleApplication = React.createClass({ render: function() { var appName = this.props.appName; var message = 'React has been successfully running.' + ' Appname is: ' + appName; return React.DOM.p(null, message); } }); var ExampleApplicationFactory = React.createFactory(ExampleApplication); React.render( ExampleApplicationFactory({appName: 'Demo'}), document.getElementById('container') ); </script> </body> </html>
props & state • props 是各 component 之间传递的数据,不 可变 •
state 是 component 内部的属性
None
Flux • ⼀一种应⽤用设计⽅方式,适⽤用性⼲⼴广泛,不仅⽤用于 react • 区别于 MVC,提出 action dispatcher store
view 的概念,数据单向传输
None
None
None
Action • 定义语意化的的⽅方法,被 view 的 component 调⽤用 • 数据由 view
传到 action
Dispatcher • 应⽤用的枢纽,由 action 调⽤用 • 数据由 action 传⼊入 dispatcher
• new Dispatcher() -> dispatch()
Store • 通过调⽤用 dispatcher 注册 handle • Dispatcher.register(function(payload ){}) •
数据 payload 由 dispatcher 传⼊入 store
View • 从 store 获取数据,并监听 store 的变化 • 数据由 store
传⼊入 view • 当 view 通过 setState 改变 state 时,重 新渲染 • 当有浏览器事件时,调⽤用 action 对应⽅方法
Unidirectional data flow
Flux 的例⼦子 • React docs
Tools • chrome devtools
Tools
Q&A
Thanks!