在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ getInitialState 里的 Props 是一個(gè)反模式
顯示數(shù)據(jù)
組件的引用
Controlled Input 值為 null 的情況
Reconciliation
子 props 的類型
組件的詳細(xì)說(shuō)明和生命周期
傳遞 Props
特殊的非 DOM 屬性
組件 API
PureRenderMixin
雙向綁定輔助工具
瀏覽器中的工作原理
深入 JSX
表單組件
Dangerously Set innerHTML
入門(mén)
JSX 中的 If-Else
克隆組件
教程
更多的關(guān)于Refs
JSX 的 false 處理
高級(jí)性能
Mounting 后 componentWillReceiveProps 未被觸發(fā)
簡(jiǎn)介
測(cè)試工具集
JSX 陷阱
工具集成(ToolingIntegration)
公開(kāi)組件功能
通過(guò) AJAX 加載初始數(shù)據(jù)
事件系統(tǒng)
可復(fù)用組件
this.props.children undefined
不可變數(shù)據(jù)的輔助工具(Immutability Helpers)
動(dòng)態(tài)交互式用戶界面
組件的 DOM 事件監(jiān)聽(tīng)
復(fù)合組件
動(dòng)畫(huà)
插件
JSX 展開(kāi)屬性
行內(nèi)樣式
性能分析工具
類名操作
與其他類庫(kù)并行使用 React
鍵控的片段
標(biāo)簽和屬性支持
組件間的通信
React (虛擬)DOM 術(shù)語(yǔ)
JSX 根節(jié)點(diǎn)的最大數(shù)量
在樣式props中快速制定像素值
頂層 API
深入理解 React
自閉合標(biāo)簽
為什么使用 React?
getInitialState 里的 Props 是一個(gè)反模式
與 DOM 的差異

getInitialState 里的 Props 是一個(gè)反模式

注意:

這實(shí)際上不是一篇單獨(dú)的 React 提示,因?yàn)轭愃频姆茨J皆O(shè)計(jì)也經(jīng)常會(huì)在平時(shí)的編碼中出現(xiàn);這里,React 只是簡(jiǎn)單清晰地指出來(lái)這個(gè)問(wèn)題

使用 props, 自父級(jí)向下級(jí)傳遞,在使用 getInitialState 生成 state 的時(shí)候,經(jīng)常會(huì)導(dǎo)致重復(fù)的"來(lái)源信任",i.e. 如果有可能,請(qǐng)盡量在使用的時(shí)候計(jì)算值,以此來(lái)確保不會(huì)出現(xiàn)同步延遲的問(wèn)題和狀態(tài)保持的問(wèn)題。

糟糕的例子

var MessageBox = React.createClass({
  getInitialState: function() {
    return {nameWithQualifier: 'Mr. ' + this.props.name};
  },

  render: function() {
    return <div>{this.state.nameWithQualifier}</div>;
  }
});

React.render(<MessageBox name="Rogers"/>, mountNode);

Better:

更好的寫(xiě)法:

var MessageBox = React.createClass({
  render: function() {
    return <div>{'Mr. ' + this.props.name}</div>;
  }
});

React.render(<MessageBox name="Rogers"/>, mountNode);

(For more complex logic, simply isolate the computation in a method.)

(對(duì)于更復(fù)雜的邏輯,最好通過(guò)方法將數(shù)據(jù)處理分離開(kāi)來(lái))

然而,如果你理清了這些,那么它也就 不是 反模式了。兩者兼得不是我們的目標(biāo):

var Counter = React.createClass({
  getInitialState: function() {
    // naming it initialX clearly indicates that the only purpose
    // of the passed down prop is to initialize something internally
    return {count: this.props.initialCount};
  },

  handleClick: function() {
    this.setState({count: this.state.count + 1});
  },

  render: function() {
    return <div onClick={this.handleClick}>{this.state.count}</div>;
  }
});

React.render(<Counter initialCount={7}/>, mountNode);