React_Docs_Quick Start_Notes

1. Installation

npm install --save react react-dom
Webpack as bundler
import React from 'react'; import ReactDOM from 'react-dom';

2. Hello World

ReactDOM.render( <h1>hello world</h1> document.getElementById('root') );

3. Introducing JSX

render里可以渲染element, function, class component

4. Rendering Elements

function tick() { //... ReactDOM.render( //... ); } setInterval(tick,1000);
回調(diào)函數(shù)的應(yīng)用:
It calls ReactDOM.render() every second from a setInterval() callback.
setTimeout() 參數(shù)和setInterval()一樣嘿架,但只執(zhí)行一次挠进,不存在周期概念机打。兩個函數(shù)都是異步的。tick()作為參數(shù)傳進(jìn)來裹粤,它是個回調(diào)函數(shù)如失。AJAX動態(tài)加載官撼,DOM事件愧哟,Node.js事件,鏈?zhǔn)秸{(diào)用也都用到了回調(diào)函數(shù)橡淑。

5. Components and Props

composing components

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen. All React components must act like pure functions with respect to their props.Such functions are called "pure" because they do not attempt to change their inputs, and always return the same result for the same inputs.

6. State and Lifecycle

1) Converting a function to a class:

  • Create an ES6 class with the same name that extends React.Component
  • Add a single empty method to it called render()
  • Move the body of the function into the render() method.
  • Replace props with this.props in the render() body.
  • Delete the remaining empty function declaration.

2) Adding local state to a class (move the data from props to state):

  • Replace this.props.date with this.state.date in the render() method:
  • Add a class constructor that assigns the initial this.state, pass props to the base constructor
  • Remove the date prop (<Clock date={new Date()} />)from the <Clock /> element. We will later add the timer code back to the component itself

3) Adding Lifecycle Methods to a Class:

componentDidMount() componentWillUnmount() These methods are called "lifecycle hooks". The componentDidMount() hook runs after the component output has been rendered to the DOM. This is a good place to set up a timer. If the Clock component is ever removed from the DOM, React calls the componentWillUnmount() lifecycle hook so the timer is stopped.

Clock

7. Handling Events

Toggle Click

In Javascript, class methods are not bound by default. Without bind this.handleClick and pass it to onClick, this. will be undefined when the function is actually called. Generally, if you refer to a method without ( ) after it, such as onClick={this.handleClick}, you should bind that method.

bind( )

8. Conditional Rendering

class LoginControl
functions
  • For ReactDOM.render, we only put the class <LoginControl /> in it, which has its own render(), will rendering all <*Button /> and <*Greeting />. And all key value going to be used in render() well handled in constructor().
  • constructor() of <LoginControl /> will bind this to two handlers, and initialize the state of 'isLoggedIn' used as an condition check parameter, for deciding which <*Greeting /> or <*Button /> should be rendered .
  • We use this.setState (//preveStata) to change isLoggedIn. Whenever the button clicked, isLoggedIn will be re-assign a boolean value, then this boolean value also leads to <*Greeting /> changed. Consider why initial state assigned boolean false? how this boolean firstly works in render()? how <*Greeting /> initialized?how <*Greeting /> toggled by isLoggedIn as well? (It's all about the logic behind it)
  • Take care of 'let button = null' and {button}
  • Tried to remove function UserGreeting and GuestGreeting, works. And WITHOUT EXTRACTING & ENCAPSULATION构拳,the code will be looks like this:
用handleToggleClick重做

9. Lists and Keys

map() 數(shù)組每個元素調(diào)用一個指定方法后,返回值組成新數(shù)組并返回梁棠;
forEach為數(shù)組中的每個元素執(zhí)行一次回調(diào)函數(shù)置森。

  • Keys only make sense in the context of the surrounding array.
    For example, if you extract a ListItem component, you should keep the key on the <ListItem /> elements in the array rather than on the root <li> element in the ListItem itself.

  • Keys Must Only Be Unique Among Siblings. Keys used within arrays should be unique among their siblings. However they don't need to be globally unique. We can use the same keys when we produce two different arrays.

  • A good rule of thumb is that elements inside the map() call need keys.

10. Forms

Forms Picker

<input type="text">, <textarea>, and <select> all work very similarly - they all accept a value attribute that you can use to implement a controlled component.

11. Lifting State Up

class TemperatureInput
class Calculator

Lifting state involves writing more "boilerplate" code than two-way binding approaches, but as a benefit, it takes less work to find and isolate bugs. Since any state "lives" in some component and that component alone can change it, the surface area for bugs is greatly reduced. Additionally, you can implement any custom logic to reject or transform user input.
If something can be derived from either props or state, it probably shouldn't be in the state. For example, instead of storing both celsiusValue and fahrenheitValue, we store just the last edited value and its scale. The value of the other input can always be calculated from them in the render() method. This lets us clear or apply rounding to the other field without losing any precision in the user input.

12. Composition vs Inheritance

講Composition時, 每個例子特點不同:

  1. 有個外部框架function,已經(jīng)做好了初始化符糊,比如歸屬的className, 然后里面有個{props.children}凫海,之后用到什么就往里填什么。所以外部框架的function不變男娄,隨著{props.children}的改變行贪,得到的新的function也都截然不同漾稀。
  2. 在例子1的基礎(chǔ)上,{props.children} 有被其它function代替建瘫,而不直接是JSX代碼崭捍。
  3. <WelcomeDialog /> is Make a special case of <Dialog />。這個例子暖混,就是直接給{props.children}賦值缕贡。模塊里就沒別的了。
  4. <SignUpDialog />結(jié)合了之前的幾個例子拣播。

講Inheritance時, 還提到props和state,很受用:

React deal with Inheritance:"Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.
If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it."

13. Thinking in React(filterableProductTable

另外收擦,關(guān)于ref贮配,ref={(input) => { this.textInput = input; }} />
不要濫用,state還是正路子塞赂。#ref and DOM里的幾個例子都很好泪勒。ref其實就是當(dāng)出現(xiàn)了動態(tài)的東西,有需要取值宴猾,用值得時候圆存,它幫著臨時存了下。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末仇哆,一起剝皮案震驚了整個濱河市沦辙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌讹剔,老刑警劉巖油讯,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異延欠,居然都是意外死亡陌兑,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門由捎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來兔综,“玉大人,你說我怎么就攤上這事狞玛∪沓郏” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵为居,是天一觀的道長碌宴。 經(jīng)常有香客問我,道長蒙畴,這世上最難降的妖魔是什么贰镣? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任呜象,我火速辦了婚禮,結(jié)果婚禮上碑隆,老公的妹妹穿的比我還像新娘恭陡。我一直安慰自己,他們只是感情好上煤,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布休玩。 她就那樣靜靜地躺著,像睡著了一般劫狠。 火紅的嫁衣襯著肌膚如雪拴疤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天独泞,我揣著相機(jī)與錄音呐矾,去河邊找鬼。 笑死懦砂,一個胖子當(dāng)著我的面吹牛蜒犯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播荞膘,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼罚随,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了羽资?” 一聲冷哼從身側(cè)響起淘菩,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎削罩,沒想到半個月后瞄勾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡弥激,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年进陡,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片微服。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡趾疚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出以蕴,到底是詐尸還是另有隱情糙麦,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布丛肮,位于F島的核電站赡磅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏宝与。R本人自食惡果不足惜焚廊,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一冶匹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咆瘟,春花似錦嚼隘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至灸眼,卻和暖如春卧檐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背幢炸。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工泄隔, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人宛徊。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像逻澳,于是被迫代替她去往敵國和親闸天。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容