Michael Hsu
Try to Keep everything in Mind
Michael Hsu.tw Nov, 2014
NTUIM R02 BAEIR LAB 徐承志
A JavaScript library for creating user interfaces.
Renders your UI and responds to events.
AKA: The V in MVC.
Separation of concerns
Re-render
VDOM
Rails, Express, Sails ...
Angular.js ...
Model - View - Controller 三部分的邏輯要拆開,中間鬆散的綁在一起。
高內聚 n. high cohesion
低耦合 n. low coupling
頁面上觸發 Event,有關 angular 的 data binding 進入消化循環 (watch list, dirty checking),進而操作 DOM ,re-Render 頁面。
大型專案非常多的話勒?
Web Worker?
Separation of concerns
Re-render
VDOM
點擊網頁觸發資料 Model 改變,進而影響其他的頁面的元素,頁面被動的更新難免會碰到漏掉的情況,尤其是在複雜的大型專案上。
只有變動的資料才需要放到 state
setState(data, callback)
<!DOCTYPE html>
<html>
<head>
<title>Hello React</title>
<script src="http://fb.me/react-0.12.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.12.0.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">
// ... 待會的關注點
</script>
</body>
</html>
Include React.js (0.12.0 版)
在瀏覽器將 JSX 編譯成 JS
進入點,剩下 dom 都在 react 裏生成
JSX: 在 JS 裡面寫 HTML
var HelloWorld = React.createClass({
render: function() {
return (
<p>
Hello, <input type="text" placeholder="Your name here" />!
It is {this.props.date.toTimeString()}
</p>
);
}
});
setInterval(function() {
React.render(
<HelloWorld date={new Date()} />,
document.getElementById('example')
);
}, 500);
Create a component
props: Properties
Render a ReactElement into the DOM
<!DOCTYPE html>
<html>
<head>
<title>Hello React</title>
<script src="http://fb.me/react-0.12.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.12.0.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">
// ... 待會的關注點
</script>
</body>
</html>
似曾相識?沒錯,只有進入點
var Avatar = React.createClass({
render: function() {
return (
<div>
<ProfilePic username={this.props.username} />
<ProfileLink username={this.props.username} />
</div>
);
}
});
React.render(
<Avatar username="pwh" />,
document.getElementById('example')
);
Avatar owns the
div, ProfilePic and ProfileLink instances
var ProfilePic = React.createClass({
render: function() {
return (
<img src={'http://graph.facebook.com/' + this.props.username + '/picture'} />
);
}
});
var ProfileLink = React.createClass({
render: function() {
return (
<a href={'http://www.facebook.com/' + this.props.username}>
{this.props.username}
</a>
);
}
});
an owner is the component that sets the props of other components
<!DOCTYPE html>
<html>
<head>
<title>Hello React</title>
<script src="http://fb.me/react-0.12.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.12.0.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">
// ... 待會的關注點
</script>
</body>
</html>
似曾相識?沒錯,只有進入點
var LikeButton = React.createClass({
getInitialState: function() {
return {liked: false};
},
handleClick: function(event) {
this.setState({liked: !this.state.liked});
},
render: function() {
var text = this.state.liked ? 'like' : 'unlike';
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
}
});
React.renderComponent(
<LikeButton />,
document.getElementById('example')
);
setState(data, callback) ,這個方法會把資料 data 整合進 this.state 接著重新渲染元件。
Component Lifecycle
1. Views 驅動 update(data) 事件
2. Action 將 data 送到 Dispatcher
3. Dispatcher 播送 payload 給所有 Stores
4. NodeJS’s EventEmitter 播送給 Views
5. View 跟 Store 拿 State
1
2
3
4
payload
5
1. Views 驅動 update(data) 事件
2. Action 將 data 送到 Dispatcher
3. Dispatcher 播送 payload 給所有 Stores
4. NodeJS’s EventEmitter 播送給 Views
5. View 跟 Store 拿 State
1
2
3
4
payload
5
By Michael Hsu
[椒鹽分享小聚 - 第一場] React-Flux Introduction 導讀https://www.facebook.com/events/980776715271032/?ref_dashboard_filter=upcoming