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
410
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
270
Javascript 函数表达式
zhangyuheng
2
450
Featured
See All Featured
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
The browser strikes back
jonoalderson
0
1.5k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
BBQ
matthewcrist
89
10k
Practical Orchestrator
shlominoach
191
12k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
230
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
590
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.2k
Making the Leap to Tech Lead
cromwellryan
135
10k
The SEO identity crisis: Don't let AI make you average
varn
0
530
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!